Skip to content

BUG: State not updating when calling setState on a state variable that is used in a hidden component (display: none;) #10763

@JustRightSolutionsLLC

Description

@JustRightSolutionsLLC

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions