2016-04-11 9 views
-1

私はReact.jsの初心者です。 Reactのライフサイクルを理解しようとしているうちに、私はcomponentWillReceivePropsを見つけました。私は他の機能を持っているにもかかわらず、私はまだcomponentWillReceivePropsを理解することができません。私はすべてのボタンをクリックすると、私は変数 'val'をインクリメントしている小さなスニペットを作成しました。 valが5の倍数になったとき、私は '増加'の値を変更したい、それはできない。React testing 'componentWillReceiveProps'

私のコードは次のとおりです。

var Increment = React.createClass({ 
    getInitialState: function() { 
    return {val: 0, increasing: false}; 
    }, 
    componentWillMount: function() { 
    console.log("componentWillMount"); 
    }, 
    componentDidMount: function() { 
    console.log("componentDidMount"); 
    }, 
    handleClick: function() { 
    console.log("inHandleClick"); 
    console.log(this.state.val); 
    this.setState({val: (this.state.val+1)}); 
    }, 
    componentWillReceiveProps : function(nextProps) { 
    this.setState({ 
     increasing: (nextProps.val > this.props.val) 
    }); 
    }, 
    shouldComponentUpdate: function(nextProps, nextState) { 
    return (nextState.val % 5 ==0) 
    }, 

    render: function() { 
    console.log(this.state.increasing); 
    return (
     <div> 
     <button onClick={this.handleClick}>{this.state.val}</button> 
     </div> 
    ); 
    } 
}); 


ReactDOM.render(<Increment />, mountNode); 

どれリード?事前

+0

あなたのケースでは、小道具が変更されておらず、状態のみが変化しているため、 'componentWillReceiveProps'は呼び出されていません。おそらく、あなたは 'componentWillUpdate'を使うつもりでしたか?これは小道具や状態が変わると呼び出されます。 – Aaron

+0

アーロン...あなたは私の例を助けてくれますか? –

+0

@SukhmeetSingh私はあなたがReactの状態と小道具の違いに混乱していると信じています。 [this](http://stackoverflow.com/questions/27991366/what-is-the-difference-between-state-and-props-in-react) – Calvin

答えて

1

のおかげでは、コンポーネントの小道具がセットされます場合 componentWillReceivePropsが呼ばれる(以下、より正確な説明については@Aaronのおかげで)this fiddle

var IncrementButton = React.createClass({ 
    componentWillReceiveProps : function(nextProps) { 
    this.setState({ 
     increasing: (nextProps.val > this.props.val) 
    }); 
    }, 
    render: function() { 
    return (<button onClick={this.props.handleClick}>{this.props.val}</button>); 
    } 
}); 

var Increment = React.createClass({ 
    getInitialState: function() { 
    return {val: 0, increasing: false}; 
    }, 
    handleClick: function() { 
    console.log("inHandleClick"); 
    console.log(this.state.val); 
    this.setState({val: (this.state.val+1)}); 
    }, 
    shouldComponentUpdate: function(nextProps, nextState) { 
    return (nextState.val % 5 ==0) 
    }, 
    render: function() { 
    console.log(this.state.increasing); 
    return (
     <div> 
     <IncrementButton handleClick={this.handleClick} val={this.state.val}/> 
     </div> 
    ); 
    } 
}); 

React.render(<Increment />, mountNode); 

を参照してください。小道具の価値が変わっていなくても呼び出されることに注意してください。新しい値のpropsを古い値と比較したいので、コンポーネントの外部で状態を管理する必要があります。したがって、このサンプルでは、​​ボタンを抽出してコンポーネントを2つに分割しています。

+0

あなたがどのようにあなたの良い例を見てみたいですか? *する必要がありますが、あなたの 'componentWillReceiveProps'の説明は真ではありません。 'componentWillRecieveProps'は、あなたのコンポーネントが再レンダリングされた場合にではなく、propsが設定されたときに呼び出される* only *です。特に、元の投稿コードは内部的に 'setState'を使用していましたが、' render'が呼び出されましたが、* only *内部状態が変更されたため、 'componentWillReceiveProps'を呼び出すことはありませんでした。 – Aaron

+0

Hmm;私はFacebookが[ライフサイクル]ページ(https://facebook.github.io/react/docs/component-specs.html#updating-componentwillreceiveprops)にあるという警告によって混乱しています。「一般的な間違いはコードの1つですこのライフサイクルメソッド中に実行され、小道具が変更されたと仮定します。 - それはcomponentWillReceivePropsが何回も呼び出されるように聞こえるし、それらの呼び出しのサブセットだけが実際に変更された小道具を反映します。 – mjohnsonengr

+0

間違いは、小道具が*セット*されているので何とか*変更されていると仮定していますが、以前と同じ小道具と同じである可能性があります。しかし、* props *が決して設定されていないので、コンポーネントが* state *のみをセットすると再描画されますが、* componentWillReceivePropsは呼び出されません。対照的に、 'componentWillUpdate'は* propsまたはstateが設定されている場合に呼び出されます。 – Aaron