jeudi 7 juillet 2016

React: how to force state changes to take place after setState

How to force the state update to take place in React after calling setState?

As far as I know, state is effectively updated when render is called next time. Also, forceUpdate() should re-render the component and update the state changes immediately, right? However this doesn't seem to be the case. Here's an example.

handleSomeEvent(data) {
    this.setState({
        data: data
    })
    this.forceUpdate()

    // At this point state should be updated but it isn't
    this.props.consumeData(this.state.data) // consumeData will receive old data
}

How do I ensure state is actually updated before calling consumeData?

Aucun commentaire:

Enregistrer un commentaire