Skip to content
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

πŸ› οΈ Mutation μ™„λ£Œ ν›„ μ„œλ²„ μƒνƒœ 동기화 #58

Merged
merged 3 commits into from
May 14, 2024

Conversation

BangDori
Copy link
Collaborator

@BangDori BangDori commented May 14, 2024

μž‘μ—… 이유


μž‘μ—… 사항

1️⃣ Mutation μ„œλ²„ 동기화

  • 기쑴에 Mutation을 μ™„λ£Œν•œ 이후 μ„œλ²„ μƒνƒœμ— λŒ€ν•œ 동기화λ₯Ό μ§„ν–‰ν•˜κ³  μžˆμ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.

μ’‹μ•„μš” API 호좜 후에 μ„œλ²„μ™€μ˜ 동기화가 μ§„ν–‰λ˜μ§€ μ•ŠλŠ” λͺ¨μŠ΅

API 호좜이 μ™„λ£Œλœ 이후 μ„œλ²„ μƒνƒœμ™€ 동기화λ₯Ό μ§„ν–‰ν•˜μ§€ μ•Šκ³  있기 λ•Œλ¬Έμ—, μ‚¬μ΄λ“œ μ΄νŽ™νŠΈλ‘œ μ„œλ²„μ™€μ˜ μƒνƒœκ°€ λΆˆμΌμΉ˜ν•˜λŠ” ν˜„μƒμ΄ λ°œμƒν•  수 μžˆμŠ΅λ‹ˆλ‹€.

이λ₯Ό ν•΄κ²°ν•˜κΈ° μœ„ν•΄ κΈ°μ‘΄ mutation hooks 에 onSettled μ˜΅μ…˜μ„ ν™œμš©ν•˜μ—¬, mutation이 μ™„λ£Œλ  경우 ν•΄λ‹Ή 쿼리듀을 λ¬΄νš¨ν™”μ‹œμΌœ λ‹€μ‹œ μ„œλ²„λ‘œλΆ€ν„° λ°›μ•„μ˜€λ„λ‘ μˆ˜μ •ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

export const useLikes = (feedId: number, isLiked: boolean) => {
  const queryClient = useQueryClient();

  const { mutate: handleLikeFeed, isPending } = useMutation({
    mutationFn: () => ...
    onMutate: async () => ...
    onError: (_, __, context) => ...
    onSuccess: (response, _, context) => ...
    onSettled: () => {
      // ✨ μ’‹μ•„μš” APIκ°€ 호좜 된 이후에 μ„œλ²„ μƒνƒœμ™€μ˜ 동기화λ₯Ό μœ„ν•΄ 쿼리λ₯Ό λ¬΄νš¨ν™”ν•œλ‹€.
      queryClient.invalidateQueries({ queryKey: [QUERY_KEYS.feeds] });
      queryClient.invalidateQueries({ queryKey: [QUERY_KEYS.feed, feedId] });
    },
  });

  return { handleLikeFeed, isPending };
};

μ’‹μ•„μš” API 호좜 후에 μ„œλ²„μ™€μ˜ 동기화가 μ§„ν–‰λ˜λŠ” λͺ¨μŠ΅

2️⃣ Mutation 쿼리 λ¬΄νš¨ν™”

기쑴에 쿼리 λ¬΄νš¨ν™”λ₯Ό ν”Όλ“œ 상세 νŽ˜μ΄μ§€λ₯Ό μƒκ°ν•˜μ—¬ μ•„λž˜μ˜ 두 쿼리λ₯Ό μ΄ˆκΈ°ν™”ν•˜κ³  μžˆμ—ˆμŠ΅λ‹ˆλ‹€.

  1. QUERY_KEYS.feeds
  2. [QUERY_KEYS.feed, feedID]

곡식 λ¬Έμ„œλ₯Ό ν™•μΈν–ˆμ„ λ•Œ, 쿼리 λ¬΄νš¨ν™”μ˜ startWithλ₯Ό 가지고 λͺ¨λ“  쿼리λ₯Ό λ¬΄νš¨ν•œλ‹€κ³  λ‚˜μ™€μžˆμŠ΅λ‹ˆλ‹€. κ·Έλž˜μ„œ ν”Όλ“œ λ””ν…ŒμΌμ— λŒ€ν•œ 쿼리λ₯Ό QUERY_KEY.feed, feedId -> QUERY_KEY.feeds, feedId 둜 μˆ˜μ •ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

export const useLikes = (feedId: number, isLiked: boolean) => {
  const queryClient = useQueryClient();

  const { mutate: handleLikeFeed, isPending } = useMutation({
    mutationFn: () => ...
    onMutate: async () => ...
    onError: (_, __, context) => ...
    onSuccess: (response, _, context) => ...
    onSettled: () => {
      // ✨ QUERY_KEYS.feeds둜 μ‹œμž‘ν•˜λŠ” λͺ¨λ“  쿼리λ₯Ό λ¬΄νš¨ν™”ν•œλ‹€.
      queryClient.invalidateQueries({ queryKey: [QUERY_KEYS.feeds] });
    },
  });

  return { handleLikeFeed, isPending };
};

리뷰어가 μ€‘μ μ μœΌλ‘œ 확인해야 ν•˜λŠ” λΆ€λΆ„

  • κΈ°μ‘΄ μ½”λ“œμ˜ 문제 ν•΄κ²° 방식에 λŒ€ν•΄ κ°œμ„  사항이 μžˆλ‹€λ©΄ νŽΈν•˜κ²Œ λ§ν•΄μ£Όμ„Έμš”!
  • κΈ°μ‘΄ μ½”λ“œμ˜ λ¬Έμ œμ μ„ μ΄ν•΄ν•˜μ˜€λ‚˜μš”?

λ°œκ²¬ν•œ 이슈

  • μ—†μŒ

@BangDori BangDori requested a review from Legitgoons May 14, 2024 11:48
@BangDori BangDori self-assigned this May 14, 2024
Copy link

This pull request is automatically being deployed by Amplify Hosting (learn more).

Access this pull request here: https://pr-58.d37mn03xh3qyyz.amplifyapp.com

Copy link
Member

@Legitgoons Legitgoons left a comment

Choose a reason for hiding this comment

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

κΈ°μ‘΄ μ½”λ“œμ˜ 문제점과 ν•΄κ²° 방식에 λŒ€ν•΄ μ΄ν•΄ν–ˆμŠ΅λ‹ˆλ‹€. λ³‘ν•©ν•˜μ…”λ„ μ’‹μŠ΅λ‹ˆλ‹€.

@BangDori BangDori merged commit c90845d into main May 14, 2024
2 checks passed
@BangDori BangDori deleted the fix/issue-56-mutation branch May 14, 2024 13:12
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.

2 participants