Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

342
Views
queryClient.setQueryData react query return undefined

i'm working with react query queryClient, i want to add new data from result of mutation to previous data with key ["feed", id], i combined res and previous to newQuestions and when i console.log newQuestions i get the right data, but when i put that to queryClient.setQueryData it returned undefined

const { isLoading, mutate } = useMutation(createQuestion, {
onSuccess: (res) => {
  //get prev data
  const previousQuestion = queryClient.getQueryData(["feed", id]).data;
  
  //combine
  const newQuestions = [res, ...previousQuestion];
  console.log(newQuestions);

  //put
  queryClient.setQueryData(["feed", id], newQuestions);

}});

however if i put res or previous to queryClient.setQueryData it works

const { isLoading, mutate } = useMutation(createQuestion, {
onSuccess: (res) => {
  //put
  queryClient.setQueryData(["feed", id], res);  //it works 

}});

and

const { isLoading, mutate } = useMutation(createQuestion, {
onSuccess: (res) => {
  //put
  queryClient.setQueryData(["feed", id], (previous)=>previous); //it works

}});

but

const { isLoading, mutate } = useMutation(createQuestion, {
onSuccess: (res) => {
  //put
  queryClient.setQueryData(["feed", id], (previous)=>[res,...previous]);    //doesnt work

}});
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!