1. react-query 특정 조건일 때 fetch하도록 하기

  • 이전부터 찾아 해매던 기능... 역시 영어로 stack overflow 보는게 최고 빠르다...
  • document 예제와 같이, query option으로 enabled(boolean) 값을 주면 됨!! 
    • enabled로 준 값이 true일 때만 fetch함
    •  // Get the user
       const { data: user } = useQuery(['user', email], getUserByEmail)
       
       const userId = user?.id
       
       // Then get the user's projects
       const { isIdle, data: projects } = useQuery(
         ['projects', userId],
         getProjectsByUser,
         {
           // The query will not execute until the userId exists
           enabled: !!userId,
         }
       )
       
       // isIdle will be `true` until `enabled` is true and the query begins to fetch.
       // It will then go to the `isLoading` stage and hopefully the `isSuccess` stage :)

https://react-query.tanstack.com/guides/dependent-queries

 

Dependent Queries

Subscribe to our newsletter The latest TanStack news, articles, and resources, sent to your inbox.

react-query.tanstack.com

https://stackoverflow.com/questions/63397534/conditionally-calling-an-api-using-react-query-hook

 

Conditionally calling an API using React-Query hook

I am using react-query to make API calls, and in this problem case I want to only call the API if certain conditions are met. I have an input box where users enter a search query. When the input va...

stackoverflow.com

 

+ Recent posts