rtk query without useEffect
P粉799885311
P粉799885311 2023-09-01 12:49:13
0
1
534
<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粉799885311
P粉799885311

reply all(1)
P粉670838735

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>;
};
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template