Por ejemplo tenemos tal estado:
state = { hasSomething: true, anotherKey: '', ... }
y deberíamos actualizar solo 'hasSomething':
static getDerivedStateFromProps(nextProps) { if (nextProps.accessKey === 'admin') { return { hasSomething: false, }; } return null; }¿Necesitamos destruir el prevState cuando devolvemos el nuevo estado? por ejemplo asi:
static getDerivedStateFromProps(nextProps, prevState) { if (nextProps.accessKey === 'admin') { return { ...prevState, hasSomething: false, }; } return null; }o no lo necesitamos? Revisé console.log (this.state) y devuelve todo el estado local si no hago una destrucción de prevState.
no puedo encontrar esta información en la documentación oficial de reacción :(
PD: esta lógica es solo un ejemplo :)
La documentación oficial dice It should return an object to update the state, or null to update nothing. ( https://reactjs.org/docs/react-component.html#static-getderivedstatefromprops ). No se borra el estado cuando devuelve solo una parte, por lo que funciona como setState ( https://reactjs.org/docs/react-component.html#setstate ). La documentación setState dice 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. , por lo que no necesita destruir el estado anterior.