Published on

How to Conditionally Fetch useQuery in React-Query


useQuery in react-query package is used to fetch data from api. By default, it gets triggered when the components gets mounted. However, sometime we need to conditionally query it.

It can be done by using it's enabled option. Here's how to do it.

const isAutoFetching = false;
const { isLoading, error, data } = useQuery(
  () =>
    fetch('').then(res =>
  { enabled: isAutoFetching }