setState callback is not called (React 16?)
See original GitHub issueIn short:
When testing a component which depends on the callback
of setState(updater, callback)
, it’s not working since our last react-native/react upgrade.
In detail:
Since the last react-native upgrade (which comes with react 16 alpha), a test is failing which previously worked. I report this issue here because the production code works. setState(updater, callback)
has been changed in react 16, so I assume some enzyme “glue code” does not work anymore.
I’ve created a snippet which shows the problem:
import React, { Component } from 'react';
import { shallow } from 'enzyme';
import { TextInput } from 'react-native';
class MyTextInput extends Component {
constructor(props) {
super(props);
this.state = {
text: props.text,
// another prop for another TextInput...
};
}
render() {
return (
<TextInput
onChangeText={(text) => {
this.setState({ text }, () => this.props.onChangeText(this.state));
}}
value={this.state.text}
/>
// another TextInput...
);
}
}
describe('MyTextInput', () => {
it('shows the bug', () => {
const onChangeMock = jest.fn();
const textField = shallow(<MyTextInput text={'41'} onChangeText={onChangeMock} />);
textField.simulate('ChangeText', '42');
expect(textField.state('text')).toBe('42'); // OK - setState's updater works
expect(onChangeMock).toHaveBeenCalledTimes(1); // NOT OK - setState's callback is not working
expect(onChangeMock).toHaveBeenCalledWith({ text: '42' }); // NOT OK
});
});
Relevant dependencies:
- enzyme@2.8.2
- react@16.0.0-alpha.6
- react-native@0.44.0
Thank you in advance!
Issue Analytics
- State:
- Created 6 years ago
- Comments:12 (3 by maintainers)
Top Results From Across the Web
setState callback not working as expected
I have a working app and am just trying to set the state to loading: true while my app makes a submission call...
Read more >How to Use the setState Callback in React
To perform an action in a React component after calling setState, such as making an AJAX request or throwing an error, we use...
Read more >React setState callback function - Code Gino
Pass a callback function to setState if it might be called multiple times and the new state needs to be calculated based on...
Read more >How to use callbacks to set State in React? - #13 - YouTube
Welcome to Learning at Lambert Labs session #13. This week, Guy King, explains how React updates State asynchronously in a component.
Read more >You called `setState` with a callback that isn't callable-Reactjs
The second (optional) parameter to setState is a callback function, not a string. You can pass a function that will be executed once...
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
No solution with enzyme? Using
react-test-renderer
would completely change the test setup.@ljharb Well, I guess this problem doesn’t cause by enzyme, because I didn’t use it in my project.