[ReactNative_Study_9]React_query
1) Reat_query
https://react-query.tanstack.com/overview
Overview
Overview React Query is often described as the missing data-fetching library for React, but in more technical terms, it makes fetching, caching, synchronizing and updating server state in your React applications a breeze. Motivation Out of the box, React a
react-query.tanstack.com
사용법
1) App.js 에 가서 queryClient 를 만든다음에 사용하는 컴포넌트 전체를 QueryClientProvider 로 감싸준다.
import { QueryClient, QueryClientProvider, useQuery } from 'react-query'
const queryClient = new QueryClient()
export default function App() {
return (
<QueryClientProvider client={queryClient}>
<Example />
</QueryClientProvider>
)
}
const { isLoading, error, data } = useQuery('repoData',
() => fetch('https://api.github.com/repos/tannerlinsley/reactquery').then(res => res.json()))
그러나 useQuery를 쓰면 data가 cached 되어 있다. 즉, data를 보관하기 위해서 별도로 작성하지 않아도 한번 fetch한 data는 날라가지 않고 보관된다. 이를 가능하게 해주는 것이 'repoDdata'라고 쓰여져 있는 부분의 key이다. key 이름으로 React query Cached 시스템에 저장되어 있기 때문에 동일한 key를 가지면 다시 fetch하지 않는 것이다.
useQuery
Subscribe to our newsletter The latest TanStack news, articles, and resources, sent to your inbox.
react-query.tanstack.com
ueQuery 자체에는 다양한 값을 받아 올 수 있음. 링크 참조
4) QueryClinet
--> 위에서 받아온 data를 key에 넣어서 cached 할 떄, 모든 데이터를 관장하는 것을 queryClient이며 특정 key 저장시에 string 또는 array 형태로 저장시켜, 카테고리로 구분 할 수도 있다.
ex) ["todos" , "goMountatin"]
["todos" , "goSchool"]
// 이는 각각의 항목은 goMountain / go SChool 이지만 'todos'라는 항목으로 카테고리하여 저장한 것과 같다.
// 이때 queryClint는 key로 저장되어 있는 값들을 현재 페이지에서 refecting 하거나 다른 스크린에서 불러올 수 있다.
// 위에서 'todos' 이름이 들어간 data를 refecthing 함.
const queryClient=useQueryClient();
// refetch all active queries partially matching a query key:
await queryClient.refetchQueries(['todos'])
https://react-query.tanstack.com/reference/QueryClient
QueryClient
Subscribe to our newsletter The latest TanStack news, articles, and resources, sent to your inbox.
react-query.tanstack.com