I am new to the reactor. When changing state after setState({isDisabled: this.props.isDisabled}) I still get the old value in this.state.isDisabled . What am I doing wrong?

 const ButtonRegister = React.createClass({ componentWillReceiveProps() { if (this.props.isDisabled !== undefined) { this.setState({isDisabled: this.props.isDisabled}); console.log('props vs state: ', this.props.isDisabled, this.state.isDisabled); } }, render() { return ( <button disabled={this.props.isDisabled} << 1 disabled={this.state.isDisabled} << 2 type="button">Registration</button> ) } }); 

Those. console.log() shows different values ​​for state and props , and the button, if you leave the << 2 option in its state, is late.

In other words, I wanted to intercept props.isDisabled and force it into state . But after setState() control does not redraw. Or am I somehow changing the state ?

  • No need to copy the props in the state - then with the changes it will be bad. - Qwertiy

1 answer 1

As far as I understand, you want to set the state value from props , but the componentWillReceiveProps method only warns you that the props will be changed, so when you call this.props in this method, you get the old values, the new values are passed as nextProps argument, which gets this method.

I.e:

 componentWillReceiveProps(nextProps) { //nextProps - это и есть новое значение } 

Link to the documentation.

Note that this method is not called during the initial render.