警告:componentWillReceiveProps已被重命名,不建议使用

时间:2019-12-20 17:16:32

标签: react-native navigation components yarn npx

我使用createBottomTabNavigator

在我的课堂上

const Tab = createBottomTabNavigator({
  MainScreen: { screen: MainScreen },
  SecondScreen: { screen: SecondScreen }
});

const Container = createAppContainer(Tab);

export default class App extends React.Component {
  state = {
    isReady: false
  };
  componentDidMount = () => {
    this._load();
  };
  render() {
    const { isReady } = this.state;
    if (!isReady) {
      return <AppLoading />;
    }
    return <Container />;
  }
  _load = async () => {
    try {
      this.setState({ isReady: true });
    } catch (error) {}
  };
}

这是我的package.json

"dependencies": {
    "expo": "~36.0.0",
    "react": "^16.12.0",
    "react-dom": "~16.9.0",
    "react-native-gesture-handler": "^1.5.2",
    "react-native-web": "~0.11.7",
    "react-navigation": "3.0.8"
  }

这是我的错误

Warning: componentWillReceiveProps has been renamed, and is not recommended for use. 

* 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.
* Rename componentWillReceiveProps to UNSAFE_componentWillReceiveProps 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: SafeView

我做了npx react-codemod rename-unsafe-lifecycles,但是没有用。

1 个答案:

答案 0 :(得分:0)

即使在最新的React版本上使用react-navigation 3.x之后,它仍然会继续发生,因为react-navigation依赖于"@react-navigation/native": "^3.6.2",后者使用"react-native-safe-area-view":"0.11",其中SafeAreaView使用已弃用方法。

  

@ react-navigation / native的4.0.x版本使用已更新的0.14

因此,升级您的反应导航。希望能帮助到你。毫无疑问