Receive previous state in getDerivedStateFromError
Nessuno ha ancora preso questa issue.
- Lingua principale
- JavaScript
- Stelle
- 251k
- Fork
- 51.4k
- Merge medio
- 2g 4h
- PR unite (30g)
- 53
Descrizione
Do you want to request a feature or report a bug?
This is feature request.
What is the current behavior?
getDerivedStateFromError hook receives error and doesn't have access to state or component instance. This limits possible ways in which it could be used and requires to additionally use other hooks to derive the state:
class App extends Component {
state = {}
static getDerivedStateFromError(error) {
return { error }
}
static getDerivedStateFromProps(props, state) {
// do we really need this?
// the state is derived from error, not props
if (state.error)
return remapStateToPreferredStructure(state);
}
render() { /* ... */ }
}
What is the expected behavior?
getDerivedStateFromError is expected to receive previous state and have
getDerivedStateFromError(error, state)
signature to be consistent with related static hook, getDerivedStateFromProps. This getDerivedStateFromError signature is backward compatible with existing one (React 16.6.0).
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
React 16.6.0
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia dal punto di ingresso getDerivedStateFromError e confrontalo con l’hook correlato getDerivedStateFromProps. Valuta l’argomento richiesto relativo allo stato precedente rispetto alla firma esistente con un argomento; il lavoro è concluso quando il comportamento dell’API e le implicazioni di compatibilità sono chiaramente stabiliti.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- javascript, react
- Ambito
- frontend
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100