question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Receive previous state in getDerivedStateFromError

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:8
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
ljharbcommented, Aug 17, 2020

bump, literally forever

2reactions
bisubuscommented, Feb 16, 2020

Hold your horses, bot, the issue is still relevant and waits for the PR.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ReactJS getDerivedStateFromError() Method - GeeksforGeeks
This method is used to implement the Error Boundaries for the React application. It is called during the render phase, so side-effects are...
Read more >
Resetting state after getDerivedStateFromError - Stack Overflow
It seems like React should only call one of those methods, not both. Any ideas on how to take a component in and...
Read more >
React.Component
shouldComponentUpdate() is invoked before rendering when new props or state are being received. Defaults to true . This method is not called for...
Read more >
Error Handling in React Components | by Mayank Gupta
Understanding “getDerivedStateFromError” Life Cycle. This lifecycle event can be used in order to derive a new state once the error is received.
Read more >
React Error Boundaries: Complete Guide - Meticulous
Component { state = { hasError: false }; componentDidCatch(error: ... static getDerivedStateFromError(error: unknown) { // Update state so ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found