Skip to content

Receive previous state in getDerivedStateFromError #13954

Open
@bisubus

Description

@bisubus

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions