Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

107
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda