if i use setState in componentWillReceiveProps
componentWillReceiveProps(nextProps) {
this.setState({key: value}, this.doWhatever)
}
doWhatever () {
const { a } = this.props;
}
the question1 is in doWhatever this.props is new props(next props) or old props(prev props)?
the question2 is when does the nextProps become this.props?
This question I think a ask in stackoverflow.com is more helpful in here.
And in React, state and props are two concepts. Normally after your setState, the props is not change.
@ruoru React has minimal API. If you have something passed then it cannot be taken from component instance. So in componentWillReceiveProps(nextProps) this.props is prev props, in componentDidUpdate(prevProps) this.props is new props
@stephenkingsley @TrySound Thank you very much for your reply, but these React documents had what you said.
The setState callback fires when the updates have been flushed to the DOM, so new props are current at that point.
the question1 is in doWhatever this.props is new props or old props?
You'll get new props there.
the question2 is when does the nextProps become this.props?
After React re-renders the component.
Most helpful comment
The
setStatecallback fires when the updates have been flushed to the DOM, so new props are current at that point.You'll get new props there.
After React re-renders the component.