react 向子組件傳值 props中去取最新的值

 

在react中父組件在向子組件傳值時,如果父組件的值發生了改變,子組件的接收的參數值也需要接收最新的值,更新組件

這裏就需要使用componentWillReceiveProps(nextProps),

react生命週期中,可以在子組件的render函數執行前獲取新的props,從而更新子組件自己的state。

例子:

父組件向子組件傳值,子組件接收父組件參賽:

import ValutaTable from "srcDir/component/modifyValutaTable";

render () {
   <ValutaTable moneyArray={moneyArray} />
}

 componentWillReceiveProps (nextProps) {
      this.setState({
        originMoney: nextProps.moneyArray
      });
    }
  }

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章