내일배움캠프

230718 react query에서 mutate는 하나의 인자만 받는다.

Neda 2023. 7. 18. 20:37

230718 react query에서 mutate는 하나의 인자만 받는다.

 

문제 상황

react query를 생각안하고 2개의 인자를 가지는 함수를 만들었던 것이 문제였다

const addComment = async (storeId, content) => {
console.log(storeId,content) // storeId는 정상적으로 가져오지만, content 값은 undefined
....
};

  const mutationAddComment = useMutation(addComment)
  
  mutationAddComment.mutate(id,inputComment)

원인

mutate 시에는 싱글 변수 또는 객체를 인자로 받는다

mutate에서 객체로 넘기고 addComment에서 구조분해할당으로 변수를 가져오는 것으로 해결한다

In the example above, you also saw that you can pass variables to your mutations function by calling the 
mutate function with a single variable or object
-https://tanstack.com/query/v4/docs/react/guides/mutations
const addComment = async ({storeId, content}) => {
console.log(storeId,content) // 둘 다 정상적으로 가져옴
....
};

  const mutationAddComment = useMutation(addComment)
  
  mutationAddComment.mutate({ storeId: id, content: inputComment })