React-query 시리즈 4부: useQueryClient 후크를 사용하여 캐시와 상호 작용합니다.

Cover image by Lawrence Eagles in the article: What’s new in React Query 3



안녕하세요 여러분 👋

조금 시간이 걸렸지만 다시 돌아왔습니다. 격려에 항상 감사드립니다. 나의 새로운 추종자들에게 특별한 감사:
, , , , @hi_im_ml, , , 그리고 많은 다른 사람들은 명백한 제약 조건에 대해 모두 언급할 수 없습니다. 저는 이 공간을 프론트엔드 개발, 특히 react-js에 대해 이야기하는 데 사용합니다. strapi-js에 대한 주제도 다룰 예정입니다.
에서 useQuery 후크를 사용하여 데이터를 가져오고 쿼리 함수에 변수를 전달하고 마지막으로 단일 쿼리 인스턴스에서 전역 기본 구성을 재정의하는 방법에 대해 이야기했습니다.

목차


  • Intro

  • The useQueryClient hook
  • prefetchQuery
  • fetchQuery
  • getQueryData
  • refetchQueries
  • getQueryState
  • setQueryDefaults
  • clear

  • Conclusion

  • 소개

    If you have been following this series since , you may have running through your mind these questions; I have learned how to set up this library, fetch data, but how does react-query reduce or eliminate my dependence on global store library like redux? How do I get data from a query instance in the Foo.js component to the Bar.js component without using a global store or by passing props around? What if I'd like to refetch a query initially called somewhere in my app-how do I get this cache to work for me? These are the questions I intend to answer in this part of the series. We are going to see how to interact with the cache from anywhere in our application. Some of the actions we can perform on the cache include but are not limited to prefetching a query, getting the current state of a query(ries), getting query data, setting a new query default, etc.

    useQueryClient 후크

    The useQueryClient hook (to not be confused with the useQuery hook or QueryClient ) is our entry point to interacting with our query cache. The useQueryClient hook returns the instance of the current QueryClient of our application. We import useQueryClient from react-query thus

    import { useQueryClient } from 'react-query'
    
     const queryClient = useQueryClient()
    

    queryClient in the snippet above provides us a couple of methods to interact with the cache. We destructure some of these methods inline thus

    import { useQueryClient } from 'react-query'
    
     const {
        prefetchQuery,
        fetchQuery,
        getQueryData,
        refetchQueries,
        getQueryState,
        setQueryDefaults,
        clear,
      } = useQueryClient();
    
    There exist more methods from this object, but we are only taking this few I feel are most important. Do find time to look at a full list of these methods here .
    prefetchQuery

    Do you need to fetch data in some part application before the user needs it? then prefetchQuery is what you need.
    prefetchQuery is used to prefetch a query before it is needed or rendered by useQuery . By using this asynchronous method, you will spare your users the dreaded loading spinners because the query data already exists, react-query only goes to refresh this data behind the scenes if it is stale as per your QueryClient configurations.

    await prefetchQuery(queryKey, queryFn)
    

    prefetchQuery accepts a query key and a query function to prefetch.

    await prefetchQuery(queryKey)
    

    You can also pass only a query key if you have a default query function set in your QueryClient configuration.

    await prefetchQuery(queryKey, queryFn, {
         staleTime: 10000,
    })
    

    prefetchQuery also takes a configurations object as a third parameter. The snippet above will prefetch a query only when the data is older than 10 seconds.

    fetchQuery
    fetchQuery is somewhat similar to prefetchQuery . It is used for fetching and caching a query. It either returns the data from the query or throws an error. If a query exists in the cache and data is not stale, it returns the data, else it goes on to fetch the query.

    fetchQuery accepts a query key and a query function.

    try {
       const data = await fetchQuery(queryKey, queryFn)
     } catch (error) {
       console.log(error)
     }
    

    You can also pass a config object as a third parameter.

    try {
       const data = await queryClient.fetchQuery(queryKey, queryFn, {
         staleTime: 10000,
       })
     } catch (error) {
       console.log(error)
     }
    /*
    will fetch a query only when the data is older than 10 seconds.
    */
    

    fetchQuery and prefetchQuery accepts configurations options just like the useQuery hook with exception of the following : enabled , refetchInterval , refetchIntervalInBackground , refetchOnWindowFocus , refetchOnReconnect , notifyOnChangeProps , notifyOnChangePropsExclusions , onSuccess , onError , onSettled , useErrorBoundary , select , suspense , keepPreviousData , placeholderData .

    getQueryData

    This method is used to get an existing query's data. You can get data from any existing query anywhere in your application.

    const data = getQueryData(queryKey)
    

    It returns the data if it exits or undefined if it doesn't.

    const data = getQueryData(queryKey)
    
    refetchQueries refetchQueries is used to refetch queries based on a certain condition. It accepts an optional query key and/or query filter as parameters.
    Read more about query filters
    here

    // this will refetch all queries in the app:
     await refetchQueries()
    
     // this will refetch all stale queries:
     await queryClient.refetchQueries({ stale: true })
    
     /* this will refetch all active queries partially matching a query key "posts" */:
     await refetchQueries(['posts'], { active: true })
    
     // refetch all active queries exactly matching a query key:
     await refetchQueries(['posts', 1], { active: true, exact: true })
    

    getQueryState

    This function is used to get the state of an existing query. It returns undefined if this query does not exist. You can use this method to show loading, error, or success feedback for any query anywhere in your application.

    const state = getQueryState(queryKey)
    //checking if this query is currently loading.
     console.log(state.isLoading)
    
    setQueryDefaults

    This method is used to set default options for a query. It accepts a query key and an options object.

    /*
    sets a new 'cacheTime' default option for this query, overriding the global 'cacheTime' default option
    */
    
    setQueryDefaults('posts', { cacheTime: 10000})
    
     function Component() {
       const { data } = useQuery('posts')
     }
    
    clear 4916

    This methods simply clears all cache your application may be connected to.




  • We have seen that with the useQueryClient hook, we can start interacting with everything in our cache including getting a query's data, fetching a query refetching a query(ries), clearing our cache, etc.
    In the next part, we will see how can start making mutating actions to our server i.e. the create/update/delete using the useMutation hook.

    Thank you all for your support. If you are beginner and haven't written something, do that today! Please give me a 💖 if this post or part of it has helped you. Comments are welcomed too.
    Follow me on

  • Intro
  • The useQueryClient hook
  • prefetchQuery

  • fetchQuery
  • 좋은 웹페이지 즐겨찾기