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.
서버 상태를 클라이언트로 가져올 수 있다.
캐싱, 동기화, 에러핸들링 등 비동기 과정을 더욱 편하게 사용할 수 있다.
?캐싱?
데이터는 업데이트 되고 있지만 요청은 날리지 않는 것
yarn add react-query
react-query를 설치한다.
import { useQuery } from "react-query";
const { isLoading, error, data } = useQuery('mandu', () => axios("URL"));
쿼리를 사용하기 위해서 필요한 것
- 쿼리에 대한 고유한 키값 ('mandu')
- promise를 return하는 함수 (()=> axios("URL"))
키값
- 쿼리를 재패치, 캐싱, 공유하는 데 사용된다.
- 문자열도 Key가 될 수 있겠지만 컨벤션을 맞추기 위해 항상 배열을 사용한다고 한다. (['mandu'])
- (Query 내부에서 키를 배열로 변환하기 때문에 결국 같음)
쿼리의 상태(이 중 한 가지의 상태만 가짐)
- isLoading(status === 'loading')
- isError(status === 'error')
- isSuccess(status === 'success')
- isIdle(status === 'idle') - 불능상태
쿼리가 isLoading 상태면 error 속성을 사용할 수 있다. (ex. error.message)
쿼리가 success 상태면 data 속성을 사용할 수 있다.
쿼리가 가져와지는 경우 isFetching은 true다.
덮밥 먹는다!
Queries | React Query | TanStack
Queries | TanStack Query Docs
Query Basics A query is a declarative dependency on an asynchronous source of data that is tied to a unique key. A query can be used with any Promise based method (including GET and POST methods) to fetch data from a server. If your method modifies data on
tanstack.com
React Query Key 관리 | 지나가던 개발(zigae)
React Query Key 관리
queryKey는 React Query에서 중요한 개념이다. 내부적으로 데이터를 캐시하고 쿼리에 대한 종속성이 변경될 때 자동으로 다시 가져올 수 있게 한다.
www.zigae.com
'프론트엔드✏️ > 개인공부' 카테고리의 다른 글
[공통 컴포넌트,css]글자수 체크하는 Input창, x 초기화 버튼 Input, 테두리 겹치는 부분 (0) | 2022.09.06 |
---|---|
공통 컴포넌트 만드는 중 (0) | 2022.09.05 |
[알고리즘] 최소직사각형 (0) | 2022.08.20 |
부트캠프 84일차, 팀 프로젝트 발표날 (0) | 2022.08.03 |
에러잡기 (0) | 2022.08.02 |