Search code examples
reduxreact-reduxredux-toolkit

How to call Redux-toolkit-query Manually on button click


I am using Redux-toolkit-query (RTK Query) to fetch data from the server. Now, I want to call my query on button click, not automatically. I tried this but it's not working.

const { data, refetch } = useGetBusinessAreasQuery({
    enable: false,
    refetchOnWindowFocus: false,
    manual: true,
    refetchOnReconnect: false,
  });

Solution

  • You have to use the lazy query hook:

    const [ trigger, { data } ] = api.endpoints.getBuisnessAreas.useLazyQuery()
    
    const onClick = () => {
      trigger()
    }