现在好像不建议使用componentWillReceiveProps钩子了
来源:4-3 使用动态路由获取不同列表内容(3)
Liiiu
2021-03-15 13:53:01
react-dom.development.js:67 Warning: componentWillReceiveProps has been renamed, and is not recommended for use. See https://reactjs.org/link/unsafe-component-lifecycles for details.
* Move data fetching code or side effects to componentDidUpdate.
* If you're updating state whenever props change, refactor your code to use memoization techniques or move it to static getDerivedStateFromProps. Learn more at: https://reactjs.org/link/derived-state
* Rename componentWillReceiveProps to UNSAFE_componentWillReceiveProps to suppress this warning in non-strict mode. In React 18.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.
Please update the following components: NewList
有什么其他替换方法吗?
1回答
同学你好, componentWillReceiveProps改成了UNSAFE_componentWillReceiveProps,虽然改名了,但是原名字componentWillReceiveProps还是可以使用的,同学截图的是警告信息,不是报错,一般不会影响项目的正常运行,所以建议同学使用原名字,防止出现未知的错误。
祝学习愉快~
相似问题