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

98
Views
Add element to array setState React

I have useState "setAnswers" (set) and "answers" (get) (answers is array with strings)
and click trigger:

onClick = () => {
    setAnswers((prev) => [...prev, e]) 
    setValue(questionKey, answers)
    console.log(watch(questionKey))
}

but with ever click i got only previous value

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

In fact, your console.log is execute before the state finish to be calculated, if you put your console.log on an other place, normally, you find what you want.

Try it, and say me

about 4 years ago · Juan Pablo Isaza Report

0

Your console.log(watch(questionKey)) all time will go wrong on onChange.

you need use a useEffect to log or make anything else with state as base.

    useEffect(() => {
     console.log(watch(questionKey)
    }, [questionKey]);

to more info you can read here: https://dev.to/mpodlasin/react-useeffect-hook-explained-in-depth-on-a-simple-example-19ec

about 4 years ago · Juan Pablo Isaza Report

0

I think you are a little bit confused: watch function from useForm is used to

Watch specified inputs and return their values.

So console.log(watch(questionKey)) does make no sense.

watch should be used in this way:

  React.useEffect(() => {
    const subscription = watch((value, { name, type }) => console.log(value, name, type));
    return () => subscription.unsubscribe();
  }, [watch]);

You use a useEffect to subscribe/unsubscribe watch on some form fields, and then in component's body you could call:

const watchSomething = watch(<something>);

and, every time the field called <something> will change his value, watch will execute console.log(value, name, type).

If you want to print the very last value of a state variable, you should know that setValue, setAnswer are async functions and its absolutely not guaranteed that on next line you could log the last value. To solve your problem you have 2 choices:

  1. use watch correctly;

  2. forget watch and use classic useEffect:

    onClick = () => {
       setAnswers((prev) => [...prev, e]) 
       setValue(questionKey, answers)
    }
    
    useEffect(() => {
       console.log(questionKey); //<-- here you will print the very last value of questionKey
    }, [questionKey]);
    

Here a guide on how to use watch.

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!