Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

157
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda