0

I got this error while trying to prefetch the data

react_query__WEBPACK_IMPORTED_MODULE_5__.QueryClient.prefetchQuery is not a function

This is my code

 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); 

  };

I try to mutate the data through a model, I don't know what I've done wrong. I'm new to NextJs

1 Answer 1

1

There are a few things that could be causing this issue. First, make sure that you are using the latest version of react-query. Second, make sure that you are passing the correct parameters to the prefetchQuery method. The first parameter should be an array of queries, and the second parameter should be a function that returns a 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); 

  };

}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.