Receive previous state in getDerivedStateFromError
See original GitHub issueDo 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:
- Created 5 years ago
- Reactions:8
- Comments:11 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

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