最近寫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>標簽即可,這樣不論代碼是否嚴謹都不會再拋出這類警告。
希望這些方法可以幫到大家。