react / react/react

Receive previous state in getDerivedStateFromError

Aperta
#13,954 12 commenti 9 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Type: Feature Request
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

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. 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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.