2017-07-30 7 views
0

なぜ小道具がレンダリングとcomponentWillReceivePropsで異なる値を表示するのか教えてください。 buttonをクリックすると、レンダリングとcomponentWillReceivePropsの両方の関数が呼び出されますが、(this.props.val)の値が異なるのはなぜですか?ここでなぜ、小道具がrenderとcomponentWillReceivePropsで異なる値を表示するのですか?

は、任意のプロパティ値が変更され、コンポーネントがnextPropsコレクションでそれを受信するたびに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> 
) 
+4

'コンポーネントが実際に再描画するトリガーと' this.props.val'の値を変更するもので、これらの新しい小道具を、受け取る前componentWillReceiveProps'が実行されます。詳細については、[documentation](https://facebook.github.io/react/docs/react-component.html#componentwillreceiveprops)の 'componentWillReceiveProps'をご覧ください。 –

答えて

関連する問題