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