在react中父組件在向子組件傳值時,如果父組件的值發生了改變,子組件的接收的參數值也需要接收最新的值,更新組件
這裏就需要使用componentWillReceiveProps(nextProps),
react生命週期中,可以在子組件的render函數執行前獲取新的props,從而更新子組件自己的state。
例子:
父組件向子組件傳值,子組件接收父組件參賽:
import ValutaTable from "srcDir/component/modifyValutaTable";
render () {
<ValutaTable moneyArray={moneyArray} />
}
componentWillReceiveProps (nextProps) {
this.setState({
originMoney: nextProps.moneyArray
});
}
}