Skip to content

Commit d8eb0e5

Browse files
committed
fix: v4 query filters 추가 및 network mode 내용 추가, 메인 리드미 공식 문서 링크 수정
1 parent cbf1b11 commit d8eb0e5

2 files changed

Lines changed: 61 additions & 17 deletions

File tree

README.md

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# 💻 TanStack Query(aka. React Query)
1+
# 💻 TanStack Query(React Query v4)
22

3-
- 해당 저장소는 TanStack Query(aka.React Query)에서 자주 사용하는 개념들을 정리한 저장소입니다. TanStack Query의 모든 활용 방법이 작성된 상태는 아니며, 필요한 내용은 추가, 보완할 예정입니다.
4-
- 오탈자 및 가독성이 안 좋거나 수정이 필요한 내용은 `Pull Request`, `Issue` 등 자유롭게 남겨주시면 검토 후에 반영하겠습니다. 많관부 🙇‍♂️
3+
- 해당 저장소는 TanStack Query(React Query v4)에서 자주 사용하는 개념들을 정리한 저장소입니다. TanStack Query(React Query v4)의 모든 활용 방법이 작성된 상태는 아니며, 필요한 내용은 추가, 보완할 예정입니다.
4+
- 오탈자 및 가독성이 안 좋거나 수정이 필요한 내용은 `Pull Request`, `Issue` 등 자유롭게 남겨주시면 검토 후에 반영하겠습니다. 많관부 🙇‍♂️🙇‍♂️
55

66
<br />
77

@@ -11,12 +11,11 @@
1111

1212
<br />
1313

14-
## TanStack Query v4 정식 릴리즈
14+
## TanStack Query(React Query v4) 정식 릴리즈
1515

1616
![스크린샷 2022-08-17 오후 2 20 01](https://user-images.githubusercontent.com/64779472/185040681-2352e8c8-b2d7-40f7-893d-3ee2270904c9.png)
1717

18-
- react-query v4가 정식 릴리즈되면서 기존의 react-query 이름이 `TanStack Query`로 변경되었습니다.
19-
- v4는 v3의 대부분의 기능을 호환합니다. 주요 달라진 부분은 아래 문서에 간략하게 정리했습니다. 참고해주시면 감사드립니다 🙇‍♂️
18+
- React Query `v4는 v3의 대부분의 기능을 호환`합니다. 주요 달라진 부분은 아래 문서에 간략하게 정리했습니다. 참고해주시면 감사드립니다 🙇‍♂️
2019
- [TanStack Query v3 vs v4 비교 문서](https://github.com/ssi02014/react-query-tutorial/tree/master/document/v4.md)
2120

2221
<br />
@@ -61,7 +60,7 @@
6160
<br />
6261
<br />
6362

64-
## 📃 React-Query 개요 및 기능
63+
## 📃 React Query 개요 및 기능
6564

6665
### 개요
6766

@@ -84,7 +83,7 @@
8483

8584
<br />
8685

87-
## React-Query 기본 설정
86+
## React Query 기본 설정
8887

8988
[목차 이동](#주요-컨셉-및-가이드-목차)
9089

@@ -224,7 +223,7 @@ function App() {
224223

225224
[목차 이동](#주요-컨셉-및-가이드-목차)
226225

227-
- [useQuery 공식 사이트 참고](https://react-query.tanstack.com/reference/useQuery)
226+
- [useQuery v4](https://tanstack.com/query/v4/docs/react/reference/useQuery)
228227

229228
```jsx
230229
// 사용법(1)
@@ -349,9 +348,8 @@ const { status, isLoading, isError, error, data, isFetching, ... } = useQuery(
349348
- 이는 캐싱 된 데이터가 있더라도 쿼리 로딩 여부에 따라 true/false를 반환한다.
350349
- error: 쿼리 함수에 오류가 발생한 경우, 쿼리에 대한 오류 객체
351350
- isError: 에러가 발생한 경우 `true`
352-
- 그 외 리턴 데이터들을 자세히 알고 싶으면 공식 사이트 참고
353-
- [v3 useQuery](https://tanstack.com/query/v3/docs/react/reference/useQuery)
354-
- [v4 useQuery](https://tanstack.com/query/v4/docs/react/reference/useQuery)
351+
- **그 외 반환 데이터들을 자세히 알고 싶으면 useQuery 공식 사이트 문서 참고**
352+
- [useQuery v4](https://tanstack.com/query/v4/docs/react/reference/useQuery)
355353

356354
<br />
357355

@@ -383,7 +381,7 @@ const { status, isLoading, isError, error, data, isFetching, ... } = useQuery(
383381

384382
[목차 이동](#주요-컨셉-및-가이드-목차)
385383

386-
- [useQuery 공식 사이트 참고](https://tanstack.com/query/v4/docs/react/reference/useQuery)
384+
- [useQuery v4](https://tanstack.com/query/v4/docs/react/reference/useQuery)
387385
- 아래 예제들 제외하고 추가적인 옵션들은 위 사이트 참고
388386

389387
### staleTime cacheTime
@@ -404,12 +402,12 @@ const { isLoading, isFetching, data, isError, error } = useQuery(
404402

405403
<br />
406404

407-
1. staleTime (number | Infinity)
405+
1. staleTime: `(number | Infinity)`
408406
- staleTime은 데이터가 `fresh에서 stale` 상태로 변경되는 데 걸리는 시간, 만약 staleTime이 3000이면 fresh상태에서 3초 뒤에 stale로 변환
409407
- `fresh` 상태일 때는 쿼리 인스턴스가 새롭게 mount 되어도 네트워크 요청(fetch)이 일어나지 않는다.
410408
- 데이터가 한번 fetch 되고 나서 `staleTime`이 지나지 않았다면(fresh상태) unmount 후 다시 mount 되어도 fetch가 일어나지 않는다.
411409
- staleTime의 기본값은 `0`이기 때문에 일반적으로 fetch 후에 바로 stale이 된다.
412-
2. cacheTime (number | Infinity)
410+
2. cacheTime: `(number | Infinity)`
413411
- 데이터가 `inactive` 상태일 때 `캐싱 된 상태로` 남아있는 시간
414412
- 쿼리 인스턴스가 unmount 되면 데이터는 `inactive 상태로 변경`되며, 캐시는 `cacheTime`만큼 유지된다.
415413
- cacheTime이 지나면 `가비지 콜렉터`로 수집된다.
@@ -859,7 +857,7 @@ refetch({ refetchPage: (page, index) => index === 0 });
859857
860858
## useMutation
861859
862-
- [useMutation 공식 사이트](https://react-query.tanstack.com/reference/useMutation)
860+
- [useMutation v4](https://tanstack.com/query/v4/docs/react/reference/useMutation)
863861
- react-query에서 기본적으로 서버에서 데이터를 Get 할 때는 useQuery를 사용한다.
864862
- 만약 서버의 data를 post, patch, put, delete와 같이 수정하고자 한다면 이때는 useMutation을 이용한다.
865863
- 요약하자면 `R(read)는 useQuery`, `CUD(Create, Update, Delete)는 useMutation`을 사용한다.
@@ -1068,7 +1066,7 @@ const useAddSuperHeroData = () => {
10681066
10691067
[목차 이동](#주요-컨셉-및-가이드-목차)
10701068
1071-
- [useQueryErrorResetBoundary 공식 문서](https://react-query-v3.tanstack.com/reference/useQueryErrorResetBoundary)
1069+
- [useQueryErrorResetBoundary v4](hhttps://tanstack.com/query/v4/docs/react/reference/useQueryErrorResetBoundary)
10721070
- react-query에서 ErrorBoundary와 useQueryErrorResetBoundary를 결합해 `선언적`으로 에러가 발생했을 때 Fallback UI를 보여줄 수 있다.
10731071
- ErrorBoundary에 대한 설명은 해당 문서 참고 [ErrorBoundary](https://github.com/ssi02014/react-query-tutorial/blob/master/document/errorBoundary.md)
10741072

document/v4.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,52 @@ new QueryClient({
129129
});
130130
```
131131

132+
- networkMode의 설정값은 3가지가 있다.
133+
- online: 오프라인 상태에서 network 연결이 있기 전까지 fetch를 하지 않고, 이때 쿼리의 상태를 `fetchStatus:paused`로 표시한다.
134+
- always: 오프라인 상태에서도 온라인처럼 fetch를 시도합니다. 오프라인 상태에서 요청을 보내는 것이니 `status:error` 상태가 된다.
135+
- offlineFirst: `v3`에서의 동작과 같습니다. queryFn 최초 호출 후 retry를 멈춘다.
136+
137+
<br />
138+
139+
### Query Filters
140+
141+
- query filter는 query와 일치하는 특정 조건을 가진 객체다.
142+
- 지금까지 필터 옵션은 대부분 boolean flag 조합이었습니다. 하지만 이러한 flag를 조합하면 `불가능한 상태`가 발생할 수 있다.
143+
144+
```
145+
active?: boolean
146+
- When set to true it will match active queries.
147+
- When set to false it will match inactive queries.
148+
inactive?: boolean
149+
- When set to true it will match inactive queries.
150+
- When set to false it will match active queries.
151+
```
152+
153+
- 예를 들어 위와 같은 `active`, `inactive` 두 옵션은 서로 `상호 배타적`이다. 이 둘 모두를 false으로 설정한다면? 이는 말이 되지 않는다.
154+
- v4부터는 이를 type이라는 속성으로 통일시켜서 의도를 더 잘 보여줄 수 있게 됐다.
155+
156+
```
157+
- active?: boolean
158+
- inactive?: boolean
159+
+ type?: 'active' | 'inactive' | 'all'
160+
```
161+
162+
- type 속성은 기본적으로 `all`로 설정되며, active 또는 inactive 쿼리만 일치하도록 선택할 수 있다.
163+
164+
```js
165+
// Cancel all queries
166+
await queryClient.cancelQueries()
167+
168+
// Remove all inactive queries that begin with `posts` in the key
169+
queryClient.removeQueries({ queryKey: ['posts'], type: 'inactive' })
170+
171+
// Refetch all active queries
172+
await queryClient.refetchQueries({ type: 'active' })
173+
174+
// Refetch all active queries that begin with `posts` in the key
175+
await queryClient.refetchQueries({ queryKey: ['posts'], type: 'active' })
176+
```
177+
132178
<br />
133179

134180
### React18 지원

0 commit comments

Comments
 (0)