Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Receive previous state in getDerivedStateFromError #13954

Open
bisubus opened this issue Oct 24, 2018 · 12 comments
Open

Receive previous state in getDerivedStateFromError #13954

bisubus opened this issue Oct 24, 2018 · 12 comments

Comments

@bisubus
Copy link

bisubus commented Oct 24, 2018

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

@jacobrask
Copy link

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)

@hamlim
Copy link
Contributor

hamlim commented Nov 18, 2019

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.

@stale
Copy link

stale bot commented Feb 16, 2020

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.

@stale stale bot added the Resolution: Stale Automatically closed due to inactivity label Feb 16, 2020
@bisubus
Copy link
Author

bisubus commented Feb 16, 2020

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

@stale stale bot removed the Resolution: Stale Automatically closed due to inactivity label Feb 16, 2020
@stale
Copy link

stale bot commented May 16, 2020

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!

@stale stale bot added the Resolution: Stale Automatically closed due to inactivity label May 16, 2020
@ljharb
Copy link
Contributor

ljharb commented May 16, 2020

bump

@stale stale bot removed the Resolution: Stale Automatically closed due to inactivity label May 16, 2020
@stale
Copy link

stale bot commented Aug 16, 2020

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!

@stale stale bot added the Resolution: Stale Automatically closed due to inactivity label Aug 16, 2020
@ljharb
Copy link
Contributor

ljharb commented Aug 17, 2020

bump, literally forever

@stale stale bot removed the Resolution: Stale Automatically closed due to inactivity label Aug 17, 2020
@stale
Copy link

stale bot commented Dec 25, 2020

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!

@stale stale bot added the Resolution: Stale Automatically closed due to inactivity label Dec 25, 2020
@ljharb
Copy link
Contributor

ljharb commented Dec 25, 2020

eternabump

@stale stale bot removed the Resolution: Stale Automatically closed due to inactivity label Dec 25, 2020
@saikota
Copy link

saikota commented Apr 12, 2022

+1 on this feature request

@JGJP
Copy link

JGJP commented Dec 11, 2023

No reply or anything for 5 years... Kind of boggles the mind

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants