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

163
Views
why we use this.initialState = this.state;

Can anyone explain to me please why using this.initialState knowing that I am using class component here in React JS ?

class Inscription extends Component {
  constructor(props) {
    super(props);

    this.state = {
      pseudo: '',
      email: '',
      password: '',

      formErrors: {}
    };

    this.initialState = this.state;

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

0

Imagine having a form with multiple inputs and eventually user submits all the data. After succesfull submit, you want to reset everything in the form. The easiest way is to do this.state = this.initialState so you don't need to define initialState two times.

Instead of adding the initialState to Class itself I would define it outside of the class.

const initialState = {
  pseudo: '',
  email: '',
  password: '',
  formErrors: {}
};

class Inscription extends Component {
  constructor(props) {
    super(props);
    this.state = initialState;
  }
  onSubmit(){
    // do backend call
    // some others events
    // eventually reset the state
    this.state = initialState;
  }
}
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!