如何删除“警告:异步存储已从本机核心中提取..."? [英] How to remove 'Warning: Async Storage has been extracted from react-native core...'?

查看:33
本文介绍了如何删除“警告:异步存储已从本机核心中提取..."?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试过此屏幕截图中推荐的内容

I've already tried what's recommended in this screenshot

通过使用这行代码import AsyncStorage from '../../../node_modules/@react-native-community/async-storage'; 在我导入 async-storage 的文件中code> 来自 react-native但此路径未解析,即此目录中不存在 async-storage .我还尝试通过运行 yarn add async-storage 来安装 async-storage(即使它已经安装),但前面提到的目录中没有任何内容

by using this line of code import AsyncStorage from '../../../node_modules/@react-native-community/async-storage'; in the file where I'm importing async-storage from react-native but this path is unresolved, i.e. async-storage doesn't exist in this directory. I also tried installing async-storage (even though it's already installed) by running yarn add async-storage, but nothing appeared in the previously mentioned directory

推荐答案

有两种方法可以做到这一点.

There are two ways you can do this.

  • 首先正确导入AsyncStorage.这将消除警告并解决问题.
  • 其次,取消警告.这只会隐藏警告,但一旦从 react-native 中删除 AsyncStorage 就会导致问题.我不会这样做,因为第一种方法实际上可以解决问题.
  • Firstly import AsyncStorage correctly. This will remove the warning and fix the problem.
  • Secondly, suppress the warning. This will just hide the warning but will cause you issues once AsyncStorage has been removed from react-native. I would not do this as the first way actually solves the problem.

注意如果您使用的依赖项使用 AsyncStorage 并且仍然以旧方式从 react-native 导入它,您会收到此警告.安装 AsyncStorage 不会修复错误.您需要查看依赖项的依赖项以找出导致该错误的原因.

Note you can get this warning if you are using a dependency that uses AsyncStorage and still imports it the old way from react-native. Installing AsyncStorage won’t fix the error. You will need to look through your dependencies’ dependencies to find out which one is causing it.

这意味着实际检查每个依赖项的代码,看看它们是否使用 AsyncStorage.搜索节点模块或依赖项的 Github 通常就足够了,但可能需要一些时间才能找到.

This means actually going through the code of each your dependencies to see if they use AsyncStorage. Searching through your node modules or on the dependency's Github usually is sufficient but it can take some time to find it.

一旦你发现是哪个原因导致了它,你应该打开一个问题或创建一个 PR 来修复依赖的 repo.在这一点上,您只能抑制警告,直到它被修复.

Once you have found out which one is causing it, you should open an issue or create a PR with a fix on the dependency’s repo. At this point suppressing the warning is all you can do until it is fixed.

安装异步存储

  1. 使用您喜欢的包管理器npmyarn
  2. 安装它
  3. 链接依赖项
  4. 使用依赖

安装:选择你常用的方法

Installation: choose the method you usually use

npm i @react-native-community/async-storage

yarn add @react-native-community/async-storage

链接依赖项(如果您使用的是 0.60+,则可能不必这样做,因为它具有 自动链接)

Link the dependency (you may not have to do this if you are using 0.60+ as it has Autolinking)

react-native link @react-native-community/async-storage

然后你像这样导入它,然后像以前一样使用它.

Then you import it like this, and use it as before.

import AsyncStorage from '@react-native-community/async-storage';

您可以通过查看这里

您可以使用以下方法抑制 YellowBox 警告>

You can supress the YellowBox warning by using the following

import {YellowBox} from 'react-native';

然后你可以添加以下内容

Then you can add the following

YellowBox.ignoreWarnings(['Warning: Async Storage has been extracted from react-native core']);

我通常在 App.js 中这样做,所以很容易跟踪我隐藏了哪些.

I usually do it in the App.js so it is easy to keep track of which ones I have hidden.

它不会从您的控制台中删除警告,但会删除与错误相关的所有 YellowBox 警告.但是,我不会在这种情况下这样做,因为有一个适当的修复方法,即正确安装依赖项.

It won't remove the warning from your console, but it will remove any YellowBox warnings associated with the error. However, I wouldn’t do this on this occasion as there is a proper fix, which is to install the dependency correctly.

目前 Expo 仍然从 react-native 导入 AsyncStorage,因此您可能仍然会遇到警告.我相信出于向后兼容性的原因,它仍然以这种方式导入它.对 Expo repo 的快速搜索显示有很多使用它的实例,您可以看到 此处.在这种情况下,您唯一的选择是取消警告.根据 Expo 功能请求,它目前正在进行,所以希望它很快就会被添加到 Expo 中.

Currently Expo still imports AsyncStorage from react-native, due to this you may still experience the warning. I believe it still imports it this way for backwards compatibility reasons. A quick search of the Expo repo shows that there are many instances where it is used as you can see here. In this case your only option would be to suppress the warning. According to the Expo feature requests it is currently in progress, so hopefully it should be added to Expo shortly.

截至 2020 年 6 月:@react-native-community/async-storage v1.11.0 可以安装在 Expo 管理的应用程序中.希望这将导致在依赖项过渡到导入异步存储的新方式时出现的这些警告更少.

As of June 2020: @react-native-community/async-storage v1.11.0 can be installed in Expo managed apps. Hopefully this will lead to less of these warnings appearing as dependencies transition to the new way of importing async-storage.

这篇关于如何删除“警告:异步存储已从本机核心中提取..."?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆