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

Shallow renderer is not handling getDerivedStateFromProps properly #13027

Closed
fatfisz opened this issue Jun 12, 2018 · 2 comments
Closed

Shallow renderer is not handling getDerivedStateFromProps properly #13027

fatfisz opened this issue Jun 12, 2018 · 2 comments

Comments

@fatfisz
Copy link
Contributor

fatfisz commented Jun 12, 2018

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

After using enzyme's setState, getDerivedStateFromProps is called with an older version of state.

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn't have dependencies other than React. Paste the link to your JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new) example below:

import { shallow } from 'enzyme';

class Demo extends Component {
  static getDerivedStateFromProps(props, state) {
    return { value: state.value };
  };

  state = { value: 'old' };

  render() {
    return <div />;
  }
}

const wrapper = shallow(<Demo />);
wrapper.setState({ value: 'new' });
assert(wrapper.state().value === 'new'); // this throws

While I don't have a standalone demo, I tracked the problem to the _updateStateFromStaticLifecycle method of ReactShallowRenderer. Inside of it getDerivedStateFromProps is called with this._instance.state, but it should be called with this._newState. This might be related to the change in when getDerivedStateFromProps is being called.

What is the expected behavior?

assert(wrapper.state().value === 'new'); // this doesn't throw

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

[email protected]
[email protected]
[email protected]

@gaearon
Copy link
Collaborator

gaearon commented Jun 12, 2018

Want to send a fix?

@fatfisz
Copy link
Contributor Author

fatfisz commented Jun 12, 2018

Sure! I'll have something in a few hrs.

@gaearon gaearon changed the title react-test-renderer is not handling getDerivedStateFromProps properly Shallow renderer is not handling getDerivedStateFromProps properly Jun 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants