๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
React

TanStack Query(react-query), useMutation์˜ mutationKey ์—ญํ• ์€ ๋„๋Œ€์ฒด ๋ญ˜๊นŒ?

by LasBe 2024. 3. 13.
๋ฐ˜์‘ํ˜•

๐Ÿ“’ react-query v5, useMutation์˜ mutationKey ์—ญํ• ์€ ๋„๋Œ€์ฒด ๋ญ˜๊นŒ?


react-query์˜  ๋ฉ”์ด์ € ๋ฒ„์ „์ด 5๋กœ ์˜ฌ๋ผ๊ฐ€๊ณ  ์‚ฌ์šฉ๋ฐฉ๋ฒ•์ด ๋งŽ์ด ๋ฐ”๋€Œ์–ด์„œ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•  ์ผ์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

๊ทธ์ € ๋‹น์—ฐ์Šค๋Ÿฝ๊ฒŒ ๊ณต์‹๋ฌธ์„œ๋ฅผ ๋ณด๊ณ  ๋ฐ”๊พธ๋˜ ์ค‘, useMutation์„ ์ˆ˜์ •ํ•  ๋•Œ mutationKey๋ผ๋Š” ์†์„ฑ์ด ๋ณด์˜€์Šต๋‹ˆ๋‹ค.

์ € Key๋ผ๋Š” ๋ง์— ๊ฝ‚ํ˜€ ๋‹น์—ฐํžˆ ํ•„์ˆ˜ ์š”์†Œ๋ผ ์ƒ๊ฐํ•œ ๋‚˜๋จธ์ง€ ํ•˜๋‚˜ํ•˜๋‚˜ ์ •์„ฑ์Šค๋Ÿฝ๊ฒŒ mutationKey๋ฅผ ์ž‘์„ฑํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

const mutation = useMutation({ mutationKey: ['mutationKey'], mutationFn: updateData });

๊ทธ๋Ÿฌ๋‹ค ์–ด๋Š ์ •๋„ ํ”„๋กœ์ ํŠธ ๊ทœ๋ชจ๊ฐ€ ์ปค์งˆ ๋ฌด๋ ต ์–ด๋Š ์ˆœ๊ฐ„ ๋ฌธ๋“ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ƒ๊ฐ์ด ๋จธ๋ฆฌ๋ฅผ ์Šค์ณค์Šต๋‹ˆ๋‹ค.

"useQuery์˜ queryKey๋Š” ์„œ๋ฒ„ ๋ฐ์ดํ„ฐ ์บ์‹ฑํ•  ๋•Œ ์“ฐ์ธ๋‹ค์ง€๋งŒ mutationKey ์–˜๋Š” ๋„๋Œ€์ฒด ๋ญ์ง€"

 

๐Ÿ“Œ mutationKey์˜ ์—ญํ• 

์–ผ๋งˆ ์—†๋Š” ์ž๋ฃŒ๋“ค์„ ์ฐพ์•„๋ณด๋‹ˆ useMutation์— ์˜ต์…˜์œผ๋กœ ๋„ฃ๋Š” mutationKey๋Š” ํฌ๊ฒŒ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ธ ๊ฐ€์ง€ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.

 

๐Ÿ”Ž setMutationDefaults / mutation ์ค‘์•™์ง‘์ค‘ํ™”

์ฒซ ๋ฒˆ์งธ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด queryClient์— setMutationDefaults๋ฅผ ์ด์šฉํ•ด key์— ๋Œ€ํ•œ ๊ธฐ๋ณธ mutation์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

queryClient.setMutationDefaults(['addUser'], { mutationFn: addUser })

 

์ด๋ ‡๊ฒŒ ๊ธฐ๋ณธ์ ์œผ๋กœ ์„ค์ •ํ•œ mutation์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด mutationFn ์—†์ด key๋กœ๋งŒ ํ˜ธ์ถœ ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

const mutation = useMutation({ mutationKey: ['addUser'] });

 

์ด๋Ÿฌํ•œ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด mutation์„ ํ•œ ๊ณณ์— ๋ชจ์•„ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ,

mutationKey ๋˜ํ•œ ํ•œ ๊ณณ์—์„œ ์ƒ์ˆ˜๋กœ ๊ด€๋ฆฌํ•ด ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํŽธ๋ฆฌํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

๐Ÿ”Ž useMutationState

import { useMutationState } from '@tanstack/react-query'

const data = useMutationState({ filters: { mutationKey: ['error'] }});

const data2 = useMutationState({
  filters: {
    status: 'error',
    mutationKey: ['error']
  },
  select: ( mutation ) => mutation
});

useMutationState ํ›…์„ ์ด์šฉํ•˜๋ฉด ํ•„ํ„ฐ ์กฐ๊ฑด์— ํ•ด๋‹นํ•˜๋Š” MutationCache ์ •๋ณด๋ฅผ ์ˆ˜์ง‘ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

filters ์†์„ฑ์— ์กฐ๊ฑด์„ ๊ฑธ ์ˆ˜ ์žˆ๋Š” ์˜ต์…˜์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • mutationKey?: MutationKey
  • exact?: boolean
  • status?: "error" | "idle" | "pending" | "success"
  • predicate?: (mutation: Mutation) => boolean

์˜ˆ์‹œ๋กœ ์™„๋ฒฝํ•˜์ง€ ์•Š์ง€๋งŒ ์ œ๊ฐ€ ErrorBoundary๋ฅผ ํ…Œ์ŠคํŠธํ•  ๋•Œ ์‚ฌ์šฉํ–ˆ๋˜ ์ฝ˜์†”์„ ์ฐ์–ด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์บ์‹œ ๋œ mutation data๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๐Ÿ”Ž useIsMutation

import { useIsMutating } from '@tanstack/react-query'
// ์–ผ๋งˆ๋‚˜ ๋งŽ์€ mutation์ด fetching ๋˜๋Š”์ง€?
const isMutating = useIsMutating()
const isMutatingPosts = useIsMutating({ mutationKey: ['posts'] })

์ด ํ›…์€ ํ•ด๋‹นํ•˜๋Š” mutation์ด ์–ผ๋งˆ๋‚˜ fetching ๋˜๋Š”์ง€ number ๊ฐ’์œผ๋กœ return ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.

์ด๊ฑด ๊ฐ„๋‹จํ•ด์„œ ์—ฌ๊ธฐ์„œ ์ด๋งŒ ์„ค๋ช…์„ ๋งˆ์น˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

๊ทธ๋Ÿผ mutationKey์— ๋Œ€ํ•œ ๊ถ๊ธˆ์ฆ์ด ํ•ด๊ฒฐ์ด ๋˜๋Š”๋ฐ ๋„์›€์ด ๋๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค!

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€


์˜คํ”ˆ ์ฑ„ํŒ