question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

setState callback is not called (React 16?)

See original GitHub issue

In 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:closed
  • Created 6 years ago
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
mabounassifcommented, Nov 15, 2017

No solution with enzyme? Using react-test-renderer would completely change the test setup.

0reactions
Soheevichcommented, May 22, 2019

@ljharb Well, I guess this problem doesn’t cause by enzyme, because I didn’t use it in my project.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found