0
你能告訴我 爲什麼道具在渲染和componentWillReceiveProps中顯示不同的值?當我點擊button
它調用功能渲染和componentWillReceiveProps,但它顯示不同的值(this.props.val)爲什麼?爲什麼道具在渲染和componentWillReceiveProps中顯示不同的值?
這裏是代碼 https://codesandbox.io/s/g5119XP2Z
class App extends Component {
update(){
render(<App val={this.props.val + 1 }/>, document.getElementById('root'));
}
componentWillReceiveProps(nextProps){
console.log(nextProps.val);
console.log("====================");
console.log(this.props.val,"val");
}
render(){
console.log("render========")
console.log(this.props.val,"val render");
return (
<div style={styles}>
<button onClick={this.update.bind(this)}>{this.props.val}</button>
<h2>Start editing to see some magic happen {'\u2728'}</h2>
</div>
)
'componentWillReceiveProps'運行之前組件實際接收的新道具,這是什麼觸發了重新渲染和改變的'this.props.val'值。你可以查看[documentation](https://facebook.github.io/react/docs/react-component.html#componentwillreceiveprops)瞭解更多信息。 –