Search code examples
reactjsreact-nativereduxrtk-query

How to call RTK query Everytime a Page Loads


I have to getAllCartItems Endpoint EveryTime The cart Page Loads , I tried to call it With useEffect but That gives Error because We cant call a hook inside a hook . Is there any other way i can call this getAllCartItems hook Everytime i open cart Page.


Solution

  • I'm not exactly sure what you mean by "every time the page loads", but if you mean "every time the component mounts", you can just set refetchOnMountOrArgChange​

    // Forcing refetch on component mount
    
    import { useGetPostsQuery } from './api'
    
    const Component = () => {
      const { data } = useGetPostsQuery(
        { count: 5 },
        // this overrules the api definition setting,
        // forcing the query to always fetch when this component is mounted
        { refetchOnMountOrArgChange: true }
      )
    
      return <div>...</div>
    }