【问题标题】:get warning "componentWillReceiveProps has been renamed" after moving expo project to react native将世博项目移动到本机反应后,收到警告“componentWillReceiveProps 已重命名”
【发布时间】:2020-05-28 22:36:39
【问题描述】:

我迁移我的 expo 项目以响应本机项目。 我删除了 expo,并尝试使用本机方式添加启动屏幕和推送通知,并在没有 expo 的情况下添加字体。 我还安装了没有博览会的原生方式的反应导航。 我使用 android studio 和 Xcode 运行该项目。 我有一些问题,但我通过在我的 package.json 文件中修复一些包版本来修复它们 现在我得到这个错误:

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

* 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. Learn more at: ***
* 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

我的旧包文件:

    "react": "16.8.3",
    "react-dom": "^16.8.6",
    "react-native": "https://github.com/expo/react-native/archive/sdk- 
     33.0.0.tar.gz",
    "react-native-autocomplete-input": "^4.1.0",
    "react-native-elements": "^1.1.0",
     ....
    "react-navigation": "^4.0.10",
    "react-navigation-drawer": "^2.3.3",
    "react-navigation-material-bottom-tabs": "^1.0.0",
    "react-navigation-stack": "^1.10.3",

My current version use safe area context with react navigation :

    "dependencies": {
    "@react-native-community/async-storage": "^1.7.1",
    "@react-native-community/masked-view": "^0.1.6",
    "@react-native-community/push-notification-ios": "^1.0.6",
    "axios": "^0.19.0",
    "buffer": "^5.4.3",
    "color": "^3.1.2",
    "libphonenumber-js": "^1.7.26",
    "moment": "^2.24.0",
    "native-base": "^2.13.5",
    "native-base-autocomplete": "^1.3.2",
    "querystring": "^0.2.0",
    "react": "16.9.0",
    "react-native": "0.61.5",
    "react-native-elements": "^1.1.0",
    "react-native-event-source": "^1.1.0",
    "react-native-gesture-handler": "^1.5.6",
    "react-native-globalize": "^3.0.0",
    "react-native-material-textfield": "^0.16.1",
    "react-native-modal-selector": "^1.1.1",
    "react-native-paper": "^2.16.0",
    "react-native-push-notification": "^3.1.9",
    "react-native-reanimated": "^1.0.1",
    "react-native-responsive-ui": "^2.1.1",
    "react-native-root-toast": "^3.1.2",
    "react-native-safe-area-context": "^0.7.2",
    "react-native-screens": "^2.0.0-beta.2",
    "react-native-slider": "^0.11.0",
    "react-native-splash-screen": "^3.2.0",
    "react-native-vector-icons": "^6.6.0",
    "react-navigation": "^4.0.10",
    "react-navigation-drawer": "^2.3.3",
    "react-navigation-material-bottom-tabs": "^1.0.0",
    "react-navigation-stack": "^1.10.3",
    "react-redux": "^7.1.0",
    "redux": "^4.0.4",
    "redux-devtools": "^3.5.0",
    "redux-devtools-extension": "^2.13.8",
    "redux-thunk": "^2.3.0",
    "search-params": "^2.1.3",
    "whatwg-url": "^8.0.0"
  },

【问题讨论】:

  • 那么有什么问题或错误吗?尝试将 componentWillReceiveProps 替换为 componentDidUpdate
  • 我收到了警告。我没有在我的任何组件中使用 componentWillReceiveProps 的问题
  • SafeView 是从哪个库导入的?它似乎是使用 componentWillReceiveProps 的那个
  • 它不在你的组件中,它可能在任何反应原生包中
  • 我真的不知道,但我认为这是使用 react-native-safe-area-context 的导航问题

标签: react-native react-navigation expo


【解决方案1】:

您应该找到使用 SafeView 对象的包。 - 从“.......”导入 SafeView-

然后你可以从 node_modules 文件夹中找到这个包文件夹。 然后你应该搜索代码并找到“componentWillReceiveProps”。 将其重命名为警告中声明的 UNSAFE_componentWillReceiveProps。

它在我的应用程序中解决了同样的问题。编码愉快!

【讨论】:

    【解决方案2】:

    我通过重新安装导航和使用的依赖项来修复这个警告,所以我当前的包文件是:

     "@react-native-community/async-storage": "^1.7.1",
    "@react-native-community/masked-view": "^0.1.6",
    "@react-native-community/push-notification-ios": "^1.0.6",
    "axios": "^0.19.0",
    "buffer": "^5.4.3",
    "color": "^3.1.2",
    "libphonenumber-js": "^1.7.26",
    "moment": "^2.24.0",
    "native-base": "^2.13.5",
    "native-base-autocomplete": "^1.3.2",
    "querystring": "^0.2.0",
    "react": "16.9.0",
    "react-native": "0.61.5",
    "react-native-elements": "^1.1.0",
    "react-native-event-source": "^1.1.0",
    "react-native-gesture-handler": "^1.5.6",
    "react-native-globalize": "^3.0.0",
    "react-native-material-textfield": "^0.16.1",
    "react-native-modal-selector": "^1.1.1",
    "react-native-paper": "^3.6.0",
    "react-native-push-notification": "^3.1.9",
    "react-native-reanimated": "^1.7.0",
    "react-native-responsive-ui": "^2.1.1",
    "react-native-root-toast": "^3.1.2",
    "react-native-safe-area-context": "^0.7.3",
    "react-native-screens": "^2.0.0-beta.2",
    "react-native-slider": "^0.11.0",
    "react-native-splash-screen": "^3.2.0",
    "react-native-vector-icons": "^6.6.0",
    "react-navigation": "^4.1.1",
    "react-navigation-drawer": "^2.3.4",
    "react-navigation-material-bottom-tabs": "^2.1.5",
    "react-navigation-stack": "^2.1.1",
    "react-redux": "^7.1.0",
    "redux": "^4.0.4",
    "redux-devtools": "^3.5.0",
    "redux-devtools-extension": "^2.13.8",
    "redux-thunk": "^2.3.0",
    "search-params": "^2.1.3",
    "whatwg-url": "^8.0.0"
    

    谢谢大家

    【讨论】:

      【解决方案3】:

      在您的情况下,这实际上不是错误。这是来自 react native 的警告。

      componentWillReceiveProps 是一个同步钩子。在此钩子中调用异步函数(如数据获取)需要在设置新道具和数据加载完成之间进行渲染。

      因此,componentWillReceiveProps 被弃用,有利于以下原因:

      1. 使用 componentDidUpdate

      所以,我建议你尽可能使用 componentDidUpdate 钩子并更新你的代码。

      • 比较 componentWillMountcomponentDidMount 时会发生类似的情况。当您需要操作异步操作时使用 componentDidMount 并在所有情况下忘记 componentWillMount

      【讨论】:

      • 我从未在我的应用中使用过 componentWillReceiveProps
      • @KhaledBoussoffara 好的 .. 检查您的第三方依赖项。我想其中一个里面有这个钩子。这就是你收到警告的原因
      • 如何找到使用此功能的第三方?
      • @KhaledBoussoffara 使用调试可以准确识别库。但是不要自己更改库代码。不要这样做,而是转到特定库的 github 并检查它们是否已迁移到新的钩子。如果不进行 PR 或添加问题 :)
      • 不错!如果这些信息是值得的,那么别忘了点赞 :)
      猜你喜欢
      • 2021-01-14
      • 2020-04-13
      • 1970-01-01
      • 2022-07-27
      • 1970-01-01
      • 1970-01-01
      • 2020-06-06
      • 2021-12-11
      • 2020-09-01
      相关资源
      最近更新 更多