React-native生命周期方法警告:“不推荐使用componentWillReceiveProps,并将在下一个主要版本中将其删除”

Sar*_*hal 9 remote-debugging react-native

在react-native中使用生命周期方法时,我在控制台以及模拟器中都遇到了以下警告消息:

警告:componentWillReceiveProps已过时,将在下一个主要版本中删除。请改用静态getDerivedStateFromProps。

警告:不推荐使用componentWillMount,它将在下一个主要版本中删除。请改用componentDidMount。作为临时的解决方法,您可以重命名为UNSAFE_componentWillMount。

警告甚至在模拟器中令人讨厌地显示,如下所示: 仿真器中的警告消息

每当我在模拟器中消除警告消息时,就会出现问题,应用程序崩溃,因此我必须再次重新启动应用程序。我该怎么办?

另外,尽管有警告消息,但我从未使用过“ componentWillReceiveProps”方法。但是,我已经使用了“ componentWillMount”方法。甚至与方法“ componentWillReceiveProps”相关的接收警告消息的原因可能是什么?

Nho*_*ynh 19

我认为这不会影响您的应用程序,这很烦人。

通过将其添加到下面来隐藏它index.js:

import { YellowBox } from 'react-native';

YellowBox.ignoreWarnings([
  'Warning: componentWillMount is deprecated',
  'Warning: componentWillReceiveProps is deprecated',
  'Module RCTImageLoader requires',
]);
Run Code Online (Sandbox Code Playgroud)