今天我们发布了 React 16.9。它包含了一些新特性、bug修复以及新的弃用警告,以便与筹备接下来的主要版本。
一、新弃用
重命名 Unsafe 生命周期方法
一年前,我们宣布 unsafe 生命周期方法重命名为:
componentWillMount
→UNSAFE_componentWillMount
componentWillReceiveProps
→UNSAFE_componentWillReceiveProps
componentWillUpdate
→UNSAFE_componentWillUpdate
React v16.9 不包含破坏性更改,而且旧的生命周期方法在此版本依然沿用。但是,当你在新版本中使用旧的生命周期方法时,会提示如下警告:
Warning:componentWillMount has been renamed,and is not recommended for use.
正如警告所示,对于每种 unsafe 的方法,通常有更好的解决方案。但你可能没有过多时间去迁移或测试这些组件。在这种情况下,我们建议运行一个自动重命名它们的 codemod 脚本:
cd your_project
npx react-codemod rename-unsafe-lifecycles
(注意:这里使用的是 npx
,不是 npm
,