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

182
Vistas
How to properly return updated state when using getDerivedStateFromProps lifecycle method?

For example we have such state:

state = { hasSomething: true, anotherKey: '', ... }

and we should update only 'hasSomething':

static getDerivedStateFromProps(nextProps) {
        if (nextProps.accessKey === 'admin') {
            return {
                hasSomething: false,
            };
        }
        return null;
    }

do we need to destruct the prevState when we return new state? for example like this:

static getDerivedStateFromProps(nextProps, prevState) {
    if (nextProps.accessKey === 'admin') {
        return {
            ...prevState,
            hasSomething: false,
        };
    }
    return null;
}

or we don't need it? i checked the console.log(this.state) and it returns the whole local state if im not make a prevState destructing.

i can't find this information in official react documentation :(

P.S. this logic just an example :)

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

0

The official documentation says It should return an object to update the state, or null to update nothing. (https://reactjs.org/docs/react-component.html#static-getderivedstatefromprops). It is not cleared the state when you return only part of it, so it works like setState (https://reactjs.org/docs/react-component.html#setstate). The setState documentation says You may optionally pass an object as the first argument to setState() instead of a function. This performs a shallow merge of stateChange into the new state., so you don't need to destruct the previous 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