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
https://stackoverflow.com/questions/63397534/conditionally-calling-an-api-using-react-query-hook
'TIL(Today I Learned) > 2022년' 카테고리의 다른 글
02.14 - input에서 한 글자 입력 후 focus를 잃는 현상 / string으로 js object 값에 접근하기 (0) | 2022.02.16 |
---|---|
01.25 - 브라우져별 지원하는 동영상 format이 다를 수 있다 (0) | 2022.01.25 |
01.20 - PM을 내려놓으며 배운 것들 (0) | 2022.01.20 |
01.19 - Custom Hook 사용 조건 / React: Too many re-renders Error (0) | 2022.01.19 |
01.14 - return type 'Element[]' is not a valid jsx element (0) | 2022.01.14 |