search

Home  >  Q&A  >  body text

rtk query without useEffect

<p>Using RTK queries, do we have to use effects to run the query on the mount? </p> <p>React Query has no effect running the query while mounted, so wondering if there is similar behavior? </p> <pre class="brush:php;toolbar:false;">const Component = ({ id }) => { const [postCheckApplication, { data }] = usePostCheckApplicationMutation(); useEffect(() => { postCheckApplication({ application_id: id }); }, [id]); console.log(data);</pre></p>
P粉799885311P粉799885311536 days ago560

reply all(1)I'll reply

  • P粉670838735

    P粉6708387352023-09-02 09:52:51

    RTK queries do not require useEffect to run the query on the mount.

    RTK queries automatically handle query execution and caching. By simply calling the generated endpoint function, you will be able to send requests and receive responses.

    Based on the above code you can optimize your code like this.

    const Component = ({ id }) => {
      const { data } = usePostCheckApplicationMutation({ application_id: id });
    
      console.log(data);
    
      return <div>Component content</div>;
    };

    reply
    0
  • Cancelreply