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

168
Views
set value to state in React

I am new to React, and I have a question of set state, this is the code:

this.setState((prevState) => {
                return{
                    prevVal: prevState.currentVal,
                    currentVal: 'DIGIT LIMIT MET'
                }
});

In this code, the prevVal in my project will be updated to 'DIGIT LIMIT MET', but I want it to be the last currentVal which is prevState.currentVal.

this.setState({
                    prevVal: this.state.currentVal,
                    currentVal: 'DIGIT LIMIT MET'

});

In this code , the problem will not appear, but according to React document, it can not make sure that the this.state.currentVal is the prevState.currentVal.

How to solve this problem perfectly? Am I misunderstanding something?

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

0

 this.setState(
      prevState => {
        const prev = prevState.currentVal;
        return {
          prevVal: prev,
          currentVal: 'DIGIT LIMIT MET'
        };
      },
      () => console.log(this.state)
    );

try this.

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!