鍍金池/ 問答/Linux  HTML/ 數(shù)據(jù)變了,子組件數(shù)據(jù)不更新,還是第一次渲染時(shí)傳的數(shù)據(jù)

數(shù)據(jù)變了,子組件數(shù)據(jù)不更新,還是第一次渲染時(shí)傳的數(shù)據(jù)

路由跳轉(zhuǎn)后,因?yàn)檫€是同一個(gè)組件,不會觸發(fā)componentDidMount,所以我在componentWillReceiveProps里進(jìn)行處理了。

回答
編輯回答
野橘

先把componentWillReceiveProps的生命周期搞清楚。
componentWillReceiveProps方法中,要通過nextProps來改變state.你setState的那個(gè)值,與nextProps毛關(guān)系都沒有,當(dāng)然子組件不更新了。
另外,props也可以render,所以不需要componentWillReceiveProps,直接在render中,根據(jù)props實(shí)現(xiàn)就好了。

2018年5月3日 15:57