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

902
Views
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 answers
Answer question

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 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!