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

216
Vistas
Why and how is state being set this way inside `getDerivedStateFromError`?

I'm learning about ErrorBoundaries in React and was wondering why and how in the getDerivedStateFromError method, is state being set this way?

// REACT
import React from 'react';

export class ErrorBoundary extends React.Component {

  state = {
    isThereError: false
  }

  static getDerivedStateFromError(error) {
    return {
      isThereError: true 
    };
  }

  // ...

};

Shouldn't it be:

// REACT
import React from 'react';

export class ErrorBoundary extends React.Component {

  state = {
    isThereError: false
  }

  static getDerivedStateFromError(error) {
    
    this.setState({ isThereError: true })

  }

  // ...

};
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

State isn't being set in the method itself. But by convention the framework expects that method to return an updated state object. The framework then internally uses that result to update state:

This lifecycle is invoked after an error has been thrown by a descendant component. It receives the error that was thrown as a parameter and should return a value to update state.

So when implementing getDerivedStateFromError in your component you don't need to worry about updating state within that method. Just return the new state.

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