Reactノート-注意すべき点

1981 ワード

Reactではこれを使用できないことに注意してください.setState()メソッド.

componentWillUpadate


問題の発生


勉強するとき、一つの例の要求は、
サンプルコードをcomponentWillUpdate()メソッドで修正し、クロックを秒0で赤のフォントに表示します.
私が最初にcomponentWillUpadate()に書いたのは
……
 componentWillUpadate:function(){
   if(this.state.time.split(':')[2] === '00'){
        this.state. = 'red';
    }
 }
……

プログラムがバグを報告した.
理由を探します.
You cannot use this.setState() in componentWillUpadate . If you need to update state in response to a prop change, use componentWillReceiveProps instead.
コードは、height="265"scrolling="no"title="LWbYKY"src="//codepen.io/ziazan/embed/LWbYKY/?height=265&theme-id=0&default-tab=js,result&embed-version=2"allowfullscreen="true">See the Pen LWbYKY by ziazan(@ziazan)on CodePen.
公式サイトではcomponentWillUpadateでは使用できません.setState()メソッド.

コンポーネントスタイルの設定


コンポーネントのstyleプロパティを設定する場合は、カッコを2つにします.
"ez-digi-clock" style={{color:this.state.color}}> {this.state.time} </div>

React , JavaScript ,