doubleyong
管理员
管理员
  • 最后登录2026-05-25
  • 发帖数1198
  • 最爱沙发
  • 喜欢达人
  • 原创写手
  • 社区居民
  • 忠实会员
阅读:4599回复:0

[react]解决react:Warning: componentWillMount has been renamed问题

楼主#
更多 发布于:2022-01-26 16:28
1. 问题描述
启动服务,发现浏览器控制台报如下warning:
VM25 common.bundle.js:36926 Warning: componentWillMount has been renamed, and is not recommended for use. See https://fb.me/react-unsafe-component-lifecycles for details.

* Move code with side effects to componentDidMount, and set initial state in the constructor.
* Rename componentWillMount to UNSAFE_componentWillMount to suppress this warning in non-strict mode. In React 17.x, only the UNSAFE_ name will work. To rename all deprecated lifecycles to their new names, you can run `npx react-codemod rename-unsafe-lifecycles` in your project source folder.

2. 问题原因

从报错的信息大致可以知道,react部分旧的生命周期(### componentWillMount)在新的版本中(react 17.0以上版本)将会弃用,需要改名字(UNSAFE_componentWillMount)。
由于在代码项目中没有使用componentWillMount生命周期,并且现在react版本还没有出到17.0(2019-11-01),为了不报这个Warning,需要解决此问题。
解决方案:
https://segmentfault.com/a/1190000020875617?utm_source=tag-newest
知识需要管理,知识需要分享
游客


返回顶部

公众号

公众号