📍참고
https://redux-toolkit.js.org/rtk-query/usage/automated-refetching#definitions
Automated Re-fetching | Redux Toolkit
RTK Query > Usage > Automated Refetching: cache invalidation management
redux-toolkit.js.org
📍Tags
정의
- re-fetching 목적으로 캐싱 및 무효화 동작을 제어하기 위해 특정 데이터 집합에 붙이는 이름
- 데이터가 mutation의 영향을 받을지 여부를 결정하기 위해 붙이는 label 개념
생성 시점
- createApi를 호출할 때 tagTypes 프로퍼티에 정의할 수 있다
- 예)
createApi({
// ...
tagTypes: ['Album', 'User'],
// ...
})
개별 tag
- 예시
{ type: "Album", id: album.userId }
- type 키 : string
- (옵셔널) id 키 : string or number
📍Providing tags (query 전용)
정의
- query는 캐시된 데이터에 tag를 갖도록 할 수 있다
- 이를 통해 쿼리에서 반환된 캐시된 데이터에 어떤 태그가 붙는지 결정된다
생성 시점
- endpoint를 만들 때, providesTags 매개 변수로 설정한다
- 예시
providesTags: (result, error, user) => [{ type: "Album", id: user.id }],
- 콜백 함수는 배열을 반환해야 한다!
콜백 함수의 매개변수
1. result : response의 result 반환
2. error : response의 error 반환
3. query argument
📍Invalidating tags (mutation 전용)
정의
- mutation은 tag를 기반으로 특정 캐시된 데이터를 무효화할 수 있다
- 이를 통해 캐시된 데이터를 다시 가져오거나 캐시에서 제거할 데이터를 결정한다
생성 시점
- endpoint를 만들 때, invalidatesTags 매개 변수로 설정한다
- providesTags와 나머지는 동일