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

106
Views
react reactquerry and formik initialvalues async problem

  const {productid} = useParams()

  const  {isLoading,isErr,data,err} =useQuery([`Admin`,productid],() =>  fetchProduct(productid))
   
  const formik = useFormik({
    initialValues: {title:data?.title,
      description:`${data?.description}`,
      price:`${data?.price}`,
      photos:data?.photos},
      onSubmit:()=>{}
    }
   )

in this code i have delay for this reason, formik every time send me null so you see the formik initial values its need to be a loaded data come.

if i can useQuerry.then(formik) my problem is solve. but you know the react rules so how i can solve this problem ?

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I don't think that a useEffect that sets values whenever data changes is the right thing to do in many cases, because with react-query, background updates can happen that might change the data and then your user input will be overwritten.

I've gone into details about a couple of approaches in my blog: https://tkdodo.eu/blog/react-query-and-forms

The simplest thing is really to split it up into two components:

const { data } = useQuery(...)

if (!data) return 'loading...'

return <Form initialData={data} />

then inside Form, you can call useFormik and set the initial values, because it will only be mounted as soon as data is available.

about 4 years ago · Santiago Trujillo Report

0

i find to solution thanks to @Sergey-Sosunov

 const  {isLoading,isErr,data,err} =useQuery([`Admin`,productid],() =>  fetchProduct(productid))

  const initialData ={title:data?.title,
    description:data?.description,
    price:data?.price,
    photos:data?.photos}

    useEffect(()=>{
formik.setValues({...initialData})
},[data])  

  const formik = useFormik({
    enableReinitialize: true,
    initialValues: initialData,
      onSubmit:()=>{}
    }
   )

about 4 years ago · Santiago Trujillo Report
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!