【问题标题】:Learning more about componentDidMount and its warning on the console了解有关 componentDidMount 及其在控制台上的警告的更多信息
【发布时间】:2020-05-11 20:48:07
【问题描述】:

我不知道为什么,但我在运行 React 应用程序时看到以下警告:

react-dom.development.js:12357 Warning: componentWillMount has been renamed, and is not recommended for use. See *documentation* 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.

Please update the following components: SideEffect(NullComponent)

我很困惑,因为我没有使用这种生命周期方法。相反,我在我的代码中使用了 useEffect、useMemo 和 useState 挂钩。我希望我有办法摆脱这个警告。

【问题讨论】:

  • 您是否在此组件中使用了其他依赖项?也请分享您的组件代码。

标签: reactjs react-hooks react-functional-component


【解决方案1】:

我也收到了这个警告。这是因为我正在使用 react-router-dom 和 react-select 内部使用 componentWillUnmount 这是警告的原因。

我做了什么?

首先我为受影响的模块做了npm update

但是,还有其他模块没有发布新版本。所以为了摆脱这个警告并看到相关的警告,我做了console.disableYellowBox = true;

这纯粹是为了我的情况。

如果您不知道这些警告,它们只会在开发模式中显示,而不是在生产模式中显示。请参阅文档here

【讨论】:

    猜你喜欢
    • 2010-11-06
    • 1970-01-01
    • 2016-10-04
    • 2015-11-26
    • 1970-01-01
    • 2021-02-21
    • 2019-04-10
    • 2022-10-15
    • 2018-07-17
    相关资源
    最近更新 更多