-
Notifications
You must be signed in to change notification settings - Fork 50.2k
Description
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
I call setState to update a state variable which is used as a prop in a component that renders a modal. If the modal is hidden (display: none;) the state variable doesn't update, if the modal is being displayed, then the state variable will update. Neither componentDidUpdate or componentWillUpdate get triggered.
If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via https://jsfiddle.net or similar (template: https://jsfiddle.net/ebsrpraL/).
Here is a snippet of my code, some of the comments point out the issue.
likeUser(userId, event) {
let itIsAMatch = false;
let users = this.state.usersToMatch;
let index = users.findIndex((user) => { return user.userId === userId; });
let userFirstName = users[index].firstName;
if (index > -1) {
// Randomly show it is a match modal (30% of the time)
let randomNumberBetween1And3 = Math.floor(Math.random() * 3) + 1;
if ((randomNumberBetween1And3 % 3) === 0 || true) {
itIsAMatch = true;
//this.setState({ matchedUserFirstName: userFirstName }); // does not update state
}
// Remove user from potential matches
users.splice(index, 1);
this.setState({
...this.state,
usersToMatch: users
});
}
if (itIsAMatch) {
//this.setState({ matchedUserFirstName: userFirstName }); // does not update state
this.toggleModalHandler('matched');
this.setState({ matchedUserFirstName: userFirstName }); // updates state
} else {
this.toggleModalHandler('liked');
}
}
The full code is part of a private repo, I'm more than happy to share access if would assist in investigating the issue.
What is the expected behavior?
Regardless of whether the modal dialog is being displayed, the state variable should update.
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
React 15.6.1
Chrome 60.0.3112.113
Windows 10 Home