enabled 란?
내가 겪었던 문제
문제를 겪었던 코드
//useuser.js
function useUser() {
const queryClient = useQueryClient();
const { data: userInfo } = useQuery({
queryKey: ["user"],
queryFn: () => api.user.getUserInfo(),
});
const { mutateAsync: signUp } = useMutation({
mutationFn: (data) => api.user.signUp(data),
});
const { mutateAsync: logIn } = useMutation({
mutationFn: (data) => api.user.logIn(data),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["user"] });
},
});
const { mutateAsync: updateUserInfo } = useMutation({
mutationFn: (formData) => api.user.updateUserInfo(formData),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["user"] });
},
});
return { userInfo, signUp, logIn, updateUserInfo };
}
export default useUser;
//Login.jsx
function Login(){
...
const { logIn } = useUser();
...
}
//SignUp.jsx
function SignUp(){
...
const { signUp } = useUser();
...
}
해결 방법
userInfo
만 파일에서 분리한다.문제를 해결한 코드
//useuser.js
function useUser(enabled = true) {
const queryClient = useQueryClient();
const { data: userInfo } = useQuery({
queryKey: ["user"],
queryFn: () => api.user.getUserInfo(),
enabled,
});
const { mutateAsync: signUp } = useMutation({
mutationFn: (data) => api.user.signUp(data),
});
const { mutateAsync: logIn } = useMutation({
mutationFn: (data) => api.user.logIn(data),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["user"] });
},
});
const { mutateAsync: updateUserInfo } = useMutation({
mutationFn: (formData) => api.user.updateUserInfo(formData),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["user"] });
},
});
return { userInfo, signUp, logIn, updateUserInfo };
}
export default useUser;
//Login.jsx
function Login(){
...
const { logIn } = useUser(false);
...
}
//SignUp.jsx
function SignUp(){
...
const { signUp } = useUser(false);
...
}
결과