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

127
Views
Is this the correct way to update a state based on the previous state in react?

Here's my code:

  const [val, setVal] = useState(0);

  function onClick() {
    setVal(val + 1);
  }

I just wanted to check if this is the correct way to update a state based on a previous state? Why/why not?

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

0

This is a perfectly fine approach. Note that you could also get the value of your current state as a callback parameter on setVal, e.g.: setVal(prevState => prevState + 1).

Just be careful when manipulating arrays or objects since they shouldn't be directly mutated (with methods such as push or pop etc).

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!