关于react.js:react-学习总结一

0次阅读

共计 1132 个字符,预计需要花费 3 分钟才能阅读完成。

标签 <React.StrictMode>

留神:严格模式查看仅在开发模式下运行;它们不会影响生产构建。

StrictMode 目前有助于:
1、辨认不平安的生命周期
2、对于应用过期字符串 ref API 的正告
3、对于应用废除的 findDOMNode 办法的正告
4、检测意外的副作用
5、检测过期的 context API

生命周期正告
过期的组件生命周期往往会带来不平安的编码实际,具体函数如下:

1、componentWillMount
2、componentWillReceiveProps
3、componentWillUpdate

16.3:为不平安的生命周期引入别名

1、UNSAFE_componentWillMount
2、UNSAFE_componentWillReceiveProps
3、UNSAFE_componentWillUpdate

ref API 的正告
参考地址:https://wuqiang.blog.csdn.net/article/details/104153645

检测副作用
渲染阶段的生命周期包含以下 class 组件办法:

1、constructor
2、componentWillMount (or UNSAFE_componentWillMount)
3、componentWillReceiveProps (or UNSAFE_componentWillReceiveProps)
4、componentWillUpdate (or UNSAFE_componentWillUpdate)
5、getDerivedStateFromProps
6、shouldComponentUpdate
7、render
8、setState 更新函数(第一个参数)

因为上述办法可能会被屡次调用,所以不要在它们外部编写副作用相干的代码,这点十分重要。疏忽此规定可能会导致各种问题的产生,包含内存透露和或呈现有效的应用程序状态。可怜的是,这些问题很难被发现,因为它们通常具备非确定性。

严格模式不能自动检测到你的副作用,但它能够帮忙你发现它们,使它们更具确定性。通过成心反复调用以下函数来实现的该操作:

1、class 组件的 constructor,render 以及 shouldComponentUpdate 办法
2、class 组件的生命周期办法 getDerivedStateFromProps
3、函数组件体
4、状态更新函数 (即 setState 的第一个参数)
5、函数组件通过应用 useState,useMemo 或者 useReducer

context API 正告

context API 正告
过期的 context API 容易出错,将在将来的次要版本中删除。在所有 16.x 版本中它依然无效,但在严格模式下,将显示以下正告:

参考文档

正文完
 0