在NextJS中使用react路由器时发生预取错误



我在尝试预取数据时出现此错误

react_query__WEBPACK_IMPORTED_MODULE_5__.QueryClient.prefetchQuery is not a function

这是我的代码

const formReducer = (state, event) => {
return {
...state,
[event.target.name]: event.target.value,
};
};
export default function InsertForm() {
const [formData, setFormData] = useReducer(formReducer, {});
const addMutation = useMutation(addOneUser,{
//fonction efa vita de mandeha ho azy le onsuccess 
onSuccess: async () =>{
//This should work, here the model is the user and the method for fetching the response is  getUser
await QueryClient.prefetchQuery(['user'], getUser);
}
})
const handleSubmit = (e) => {
e.preventDefault();
if(Object.keys(formData).length == 0) return console.log("Tsy misy donnes ao @ le formulaire");

let{nom,
mail,
dept,
status }= formData;
const model={
nom,
avatar:`https://randomuser.me/api/portraits/men/${Math.floor(Math.random()*10)}.jpg`,
mail,
dept,
status:status ?? "Active"
}
console.log(model,"mandeha tsara ny modelisation");
addMutation.mutate(model); 
};

我试图通过一个模型对数据进行变异,我不知道我做错了什么。我是NextJs 的新手

有一些因素可能会导致此问题。首先,确保您使用的是最新版本的react查询。其次,确保将正确的参数传递给prefetchQuery方法。第一个参数应该是查询数组,第二个参数应该为返回promise的函数。

const formReducer = (state, event) => {
return {
...state,
[event.target.name]: event.target.value,
};
};
export default function InsertForm() {
const [formData, setFormData] = useReducer(formReducer, {});
const addMutation = useMutation(addOneUser,{
//fonction efa vita de mandeha ho azy le onsuccess 
onSuccess: async () =>{
//This should work, here the model is the user and the method for fetching the response is  getUser
await QueryClient.prefetchQuery(['user'], getUser);
}
})
const handleSubmit = (e) => {
e.preventDefault();
if(Object.keys(formData).length == 0) return console.log("Tsy misy donnes ao @ le formulaire");

let{nom,
mail,
dept,
status }= formData;
const model={
nom,
avatar:`https://randomuser.me/api/portraits/men/${Math.floor(Math.random()*10)}.jpg`,
mail,
dept,
status:status ?? "Active"
}
console.log(model,"mandeha tsara ny modelisation");
addMutation.mutate(model); 
};

}

最新更新