react
react copied to clipboard
Receive previous state in getDerivedStateFromError
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
For a concrete use case, I am trying to refactor the following componentDidCatch
to getDerivedStateFromError
componentDidCatch(error) {
if (error instanceof RequestError) {
this.setState(
state => ({
errorsBySrc: { ...state.errorsBySrc, [error.url]: error },
}),
);
} else {
throw error;
}
}
render() {
const media = this.props.media.filter(
item => !this.state.errorsBySrc[item.url]
);
...
(media is rendered by a component that throws a RequestError
if it fails to load)
I hate to be the person that comments "+1" on an issue, however this one has been open for a bit over a year now and I wonder if we could get some insight from the React team on if this is feature request is likely to make it into React.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution.
Hold your horses, bot, the issue is still relevant and waits for the PR.
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!
bump
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!
bump, literally forever
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!
eternabump
+1 on this feature request
No reply or anything for 5 years... Kind of boggles the mind