react报错:Legacy context API has been detected within a strict-mode tree.


最近写React后台项目的时候系统总是给我报这个警告:

index.js:1 Warning: Legacy context API has been detected within a strict-mode tree.

The old API will be supported in all 16.x releases, but applications using it should migrate to the new version.

Please update the following components: LocaleReceiver

Warning: Using UNSAFE_componentWillReceiveProps in strict mode is not recommended and may indicate bugs in your code. See https://fb.me/react-unsafe-component-lifecycles for details.

虽然这些只是警告,但是看起来好难受啊,所以我上网查过了一下发现是因为我打开了React.StrictMode严格模式

StrictMode目前有助于:

当启用严格模式, React将编译一个所有使用不安全生命周期组件的列表,并打印一条关于这些组件的警告信息,就像我上面提及到的两个警告信息,如果想要取消这些警告信息,有两个办法。

方法一:

如果你不保证自己的代码无懈可击,可以参考 React严格模式(Strict Mode)中文文档,这个方法比较慢一些,因为你要严格遵循React的严格模式才会解除这些警告。

方法二:

这个方法简单粗暴,直接取消<React.StrictMode></React.StrictMode>标签即可,这样不论代码是否严谨都不会再抛出这类警告。

希望这些方法可以帮到大家。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM