react icon indicating copy to clipboard operation
react copied to clipboard

Receive previous state in getDerivedStateFromError

Open bisubus opened this issue 6 years ago • 12 comments

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

bisubus avatar Oct 24 '18 18:10 bisubus

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)

jacobrask avatar Oct 24 '18 18:10 jacobrask

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.

hamlim avatar Nov 18 '19 12:11 hamlim

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[bot] avatar Feb 16 '20 12:02 stale[bot]

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

bisubus avatar Feb 16 '20 13:02 bisubus

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[bot] avatar May 16 '20 14:05 stale[bot]

bump

ljharb avatar May 16 '20 16:05 ljharb

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[bot] avatar Aug 16 '20 07:08 stale[bot]

bump, literally forever

ljharb avatar Aug 17 '20 06:08 ljharb

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[bot] avatar Dec 25 '20 14:12 stale[bot]

eternabump

ljharb avatar Dec 25 '20 20:12 ljharb

+1 on this feature request

saikota avatar Apr 12 '22 09:04 saikota

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

JGJP avatar Dec 11 '23 05:12 JGJP