Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

909
Vistas
get value when updated,outside of formik using useRef()

I'm trying to get the value of an autocomplete field outside of my formik component everytime it changes, and i did something like this:

const formRef=useRef<any>(null);
useEffect(() => {
    console.log("eee!!",formRef.current?.values?.flagTR)
},[formRef.current?.values]);
return (
        <Formik
            
            initialValues={initialValues}
            onSubmit={handleSumbit}
            enableReinitialize={true}
            validateOnChange={true}
            validationSchema={ProduitSchema}
            innerRef={formRef}
        >

the useEffect is triggered the first time the component renders,but then when i change the value of the autocomplete the useEffect doesn't get triggered. what should i do in order to trgger useEffect everytime a value from formik changes?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Hey so I don't think the approach you are going with here is going to work, it's really not what the useRef hook is for.

The correct approach for what it looks like you want to do is to properly use Formik's context to mapValuesToProps or get access to the values, errors, and validation states.

You can use withFormik() to set up your initial form values and mapValuesToProps. Then you can use within the form component formik's useFormikContext() which will give you access to values, errors, setValues, etc

export default withFormik({
    handleSubmit: () => {},
    validateOnChange: true,
    validateOnBlur: true,
    validateOnMount: true,
    mapPropsToValues: () => ({ name: '', email: '' }),
    initialValues: { name: '', email: '' }
  })(MyFormComponent)

In the MyFormComponent you can then call useFormikContext() and do whatever you want when the values change.

const { setValues, values, isValid, validateForm } = useFormikContext()

If for some reason this is not what you want to do, or it does not solve your problem, the only way to achieve what you want in React alone is to useState and and setState each time onChange eg

const MyFormComponent = () => {
   const [nameField, nameMeta] = useField(name)    
   const [emailField, emailMeta] = useField(email)
   const [formState, setFormState] = useState({name: '', email: ''})
    

   return (
     <Formik
      enableReinitialize
      validateOnBlur={false}
      initialValues={formState}
      onSubmit={() => {}}
    >
      {props => {
         const onChange = e => {
           const targetEl = e.target
           const fieldName = targetEl.name
              setFormState({
                  ...formState,
                  [fieldName]: targetEl.value
              })
              return props.handleChange(e)
          }
    
         return (
            <>
              <input {...nameField} onChange={onChange}>
              <input {...emailField} onChange={onChange}>
            </>
         )
      </Formik>
)
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda