2

I have a user details page, and a user list page. simply you click on a link on the list page and this will render the users details on the bottom of the page. if you select another user, the component should be updated with the new data. it seems that react does not update the state, and the first select user's data is still visible.

class User extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      user: {}
    };
    this.getUser = this.getUser.bind(this);
  }

  getUser = id => {
    fetch("http://localhost:800/api/v1/users/" + id)
      .then(response => response.json())
      .then(data => this.setState({ user: data }));
    console.log("testing");
  };

  componentWillMount() {
    this.getUser(this.props.match.params.userId);
    window.state = this.state;
  }

  render() {
    const { user } = this.state;
    return (
      <div>
        <h3>User Details</h3>
        <div class="row">
          <div class="alert alert-light">
            <p>first name: {user.firstName}</p>
            <p>last name: {user.lastName}</p>
            <p>email: {user.email}</p>
            <p>birthday: {user.birthday}</p>
          </div>
        </div>
      </div>
    );
  }
}

class UserList extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      userList: []
    };
  }

  componentWillMount() {
    fetch("http://localhost:8080/api/v1/users")
      .then(response => response.json())
      .then(data => this.setState({ userList: data }));
  }

  render() {
    return (
      <div>
        <h2>Users</h2>
        {this.state.userList.map((user, key) => (
          <ul>
            <li>
              <Link to={`${this.props.match.url}/${user.id}`}>
                {user.firstName} {user.lastName}
              </Link>
            </li>
          </ul>
        ))}
        <Route path={`${this.props.match.url}/:userId`} component={User} />
        <Route
          exact
          path={this.props.match.url}
          render={() => <h3>Select a User!</h3>}
        />
      </div>
    );
  }
}

1 Answer 1

1

componentWillMount is only called before the component is mounted, and a new User component is not mounted when the userId URL parameter change.

You could add a componentDidUpdate to your User component to fetch the new user when userId change.

componentDidUpdate(prevProps){
  if (prevProps.match.params.userId !== this.props.match.params.userId) {
    this.getUser(this.props.match.params.userId);
  }
}
4
  • the url gets changed as usual, but I can still that the component does not get updated with the new data received form the api.
    – anyavacy
    Jul 17, 2018 at 17:12
  • @anyavacy Is the componentDidUpdate called? Can you console.log inside the if statement?
    – Tholle
    Jul 17, 2018 at 17:13
  • the issue now is that, the data is not shown anymore on the first click, and on page load ?
    – anyavacy
    Jul 17, 2018 at 17:17
  • @anyavacy You still need the componentDidMount and componentDidUpdate.
    – Tholle
    Jul 17, 2018 at 17:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.