您好,登錄后才能下訂單哦!
示例工程:formik-09x-component-lifecycle-events-example
核心文件:index.js
Formik大部分示例工程中都使用了極其方式創(chuàng)建Formik表單組件,本示例中則使用了復(fù)雜(或者說典型)方式創(chuàng)建一個(gè)Formik表單組件,即使用其繼承自React.Component組件,從而展示有關(guān)生命周期事件的基本使用。但是,也比較簡單,只涉及到了兩個(gè)事件:
但是,這個(gè)示例中使用的componentWillReceiveProps事件在最新的React中已經(jīng)過時(shí)。引用一個(gè)老外的話說是:
This lifecycle, however, is on the deprecation path; it will stop working as-named in React 17: https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops
Given that, we should provide a more future-friendly solution for listening for shallow route changes. Perhaps adding a popstate listener in componentDidMount. If that seems like the best option I can add it to docs, but opening this ticket for discussion/tracking.
現(xiàn)在,在最新版本React環(huán)境下需要使用UNSAFE_componentWillReceiveProps()來代替。
另外:在新版本中,官方明確指出使用上述事件容易導(dǎo)致一些復(fù)雜BUG的出現(xiàn),所以官方建議使用事件來代替。另外,還有如下建議:
Note:
Using this lifecycle method often leads to bugs and inconsistencies, and for that reason it is going to be deprecated in the future.
If you need to perform a side effect (for example, data fetching or an animation) in response to a change in props, use componentDidUpdate lifecycle instead.
For other use cases, follow the recommendations in this blog post about derived state.
If you used componentWillReceiveProps for re-computing some data only when a prop changes, use a memoization helper instead.
If you used componentWillReceiveProps to “reset” some state when a prop changes, consider either making a component fully controlled or fully uncontrolled with a key instead.
In very rare cases, you might want to use the getDerivedStateFromProps lifecycle as a last resort.
1,https://github.com/zeit/next.js/issues/4154
2,https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。