Skip to content

feat: isPending, isFetching, isLoading 차이점 추가 #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 27, 2025

Conversation

subsub-e
Copy link
Contributor

안녕하세요. TanStack Query를 공부하고 있는데, 도움이 많이 되었습니다!!
useQuery 주요 리턴 데이터부분에서 isPending / isFetching / isLoading에 대한 차이점이 추가되면 좋을꺼 같아 PR 올립니다.

@ssi02014
Copy link
Owner

ssi02014 commented Apr 25, 2025

@subsub-e 리뷰가 늦어서 죄송합니다!
react-query-tutorial에 관심을 가져주셔거 감사드리도, 좋은 의견 감사합니다.
조금 고민하다가 아래와 같이 제안드려봅니다!

  1. useQuery 주요 리턴 데이터 쪽을 개선 (isPending 추가)
- **isPending**: 쿼리가 아직 수행되지 않았고, 캐싱된 데이터가 없을 때 `true`를 반환한다.
  - status(pending)에 파생된 boolean 값이다.
- **isLoading**: 캐싱 된 데이터가 없을 때 즉, 처음 실행된 쿼리일 때 로딩 여부에 따라 `true/false`로 반환된다.
  - 이는 캐싱 된 데이터가 있다면 로딩 여부에 상관없이 `false`를 반환한다.
  - status(pending)와 fetchStatus(fetching) 결합된 boolean이다. 즉, `isFetching && isPending` 와 동일하다.
- **isFetching**: 캐싱된 데이터가 있더라도 서버에 요청을 보내면 `true`를 반환한다. 
  - fetchStatus(fetching)에 파생된 boolean 값이다.
  1. 새로운 세션은 아래와 같이 추가
💡 isPending, isLoading 주요 차이점
- isPending은 status(pending) 필드에서 직접 파생된 상태인 반면, isLoading은 status(pending)와 fetchStatus(fetching)의 결합된 상태입니다.
- 쉽게 접근하자면 isPending은 "아직 데이터가 없습니다" 를 의미합니다. 그에 반해 isLoading은 "아직 데이터가 없고, 데이터를 가져오는 중입니다"를 의미합니다.
- 이러한 차이는 enabled 옵션이 false일 때 예시로 들면 이해하가 쉽습니다. 
  - enabled가 false일 때, isPending은 true로 설정되지만, isLoading은 false로 설정됩니다.
\```ts
useQuery({ queryKey, queryFn, enabled: false });
// isPending: true, isLoading: false
\```
- [React Query v5부터는 isLoading 대신 isPending 사용을 권장합니다.](https://github.com/TanStack/query/discussions/6297#discussioncomment-7467010)
  - 로더 표시 여부는 사용 사례마다 다르지만, 대부분의 경우 isPending만으로 충분합니다. isLoading의 경우 이론적으로 "보류중이지만, 로딩되지 않은 경우(ex. enabled: false)"를 항상 검증해야 합니다.

추가적인 의견 감사드립니다

@subsub-e
Copy link
Contributor Author

피드백 적용해서 커밋 추가로 올렸습니다!!

  1. useQuery 주요 리턴 데이터에 isPending을 추가하였습니다. isLoading, isFetching 의 경우도 정보를 추가하였습니다.
  2. isLoading, isPending의 차이점도 추가하였습니다.

TanStack-Query를 처음 보는 입장에서 isLoading, isPending 의 차이점이 있으면 이해하는데 큰 도움이 된다고 생각하여 1, 2번 피드백 모두 수용하는게 좋다고 생각하는데 어떻게 생각하시는지 궁금합니다!!

Copy link
Owner

@ssi02014 ssi02014 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🚀 도움을 주신 덕분에 문서가 더 보강됐습니다 감사합니다

@ssi02014 ssi02014 merged commit cd83a62 into ssi02014:main Apr 27, 2025
@subsub-e subsub-e deleted the feat/isPending branch April 27, 2025 06:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants