我有几个场景需要存储项目AsyncStorage并将新数据推送到各自的键.其中一个是10个国家/地区的列表,AsyncStorage如下所示:
AsyncStorage.setItem('countries', JSON.stringify(countries));
Run Code Online (Sandbox Code Playgroud)
我尝试了以下内容,country我想要推送/添加到预先存在的新项目在哪里countries:
AsyncStorage.push('countries', JSON.stringify(country));
Run Code Online (Sandbox Code Playgroud)
那当然不起作用......
如上所述,我还有一些需要此功能的方案.我试图找到一个可重复使用的解决方案反应本地AsyncStorage函数来处理传入key和data,并采取设置项,或推到项目,如果它存在的照顾.
知道如何有效地完成这项功能AsyncStorage吗?
我当前的项目要求我在本地存储用户数据,所以我使用 React Native 本身的 AsyncStorage。但是我在如何检索已保存的数据方面遇到了一些问题我总是得到 null 但不知何故数据被保存..
我总是得到
{_45:0,_81:0,_65:空,_54:空}
这是我的代码,这是来自 react native 文档的简单示例
AsyncStorage.setItem('baru', 'this is new dude!!');
var b = AsyncStorage.getItem('baru');
console.log(b);
Run Code Online (Sandbox Code Playgroud) 使用IOS Simulator,React-Native AsyncStorage在哪里将数据保存在磁盘上?
我正在使用IOS Simulator 10.0版并运行IOS 10.2和react-native 0.40.0
嗨,我在向AsyncStorage中的数组添加值时遇到问题.
AsyncStorage.getItem('savedIds', (err, result) => {
const id = '1';
if (result !== null) {
console.log('Data Found', result);
result = JSON.parse(result);
result.push(id);
AsyncStorage.setItem('savedIds', JSON.stringify(result));
} else {
console.log('Data Not Found');
AsyncStorage.setItem('savedIds', id);
}
});
AsyncStorage.getItem('savedIds', (err, result) => {
console.log(result);
});
Run Code Online (Sandbox Code Playgroud)
设置初始ID后,我得到错误"result.push"不是函数.我需要更改什么来解决这个问题?还是有一个更优雅的解决方案?
我的问题来自文档:https://facebook.github.io/react-native/docs/asyncstorage.html#clear
AsyncStorage.clear - >删除所有客户端,库等的所有AsyncStorage ....
这是否意味着所有应用程序共享相同的AsyncStorage?
我正在学习react-native并尝试使用AsyncStorage. 我正在使用此代码来存储数据:
_storeData = async (token) => {
console.log("store token");
console.log(token);
try {
await AsyncStorage.setItem('token', token);
} catch (error) {
// Error saving data
console.log(error);
}
}
Run Code Online (Sandbox Code Playgroud)
调用它:this._storeData(res.token);
但它给了我这个错误:
> TypeError: Cannot read property 'setItem' of undefined
> at LoginOrCreateForm._callee$ (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:138096:71)
> at tryCatch (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21408:19)
> at Generator.invoke [as _invoke] (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21583:24)
> at Generator.prototype.(anonymous function) [as next] (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21451:23)
> at tryCatch (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21408:19)
> at invoke (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21484:22)
> at blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:21514:13
> at tryCallTwo (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:22690:7)
> at doResolve (blob:http://localhost:8081/21ec2746-a3c9-4c68-937f-ee50c63a6d58:22854:15) …Run Code Online (Sandbox Code Playgroud) 我刚刚启动了一个新的托管Expo React Native 项目(Expo SDK 35、 React Native 0.5.9)redux-persist@6.0.0,但即使是带有 redux/redux-persist 的新 expo 项目当前也会抛出以下错误。
使用AsyncStorage来自react-native,
我们得到错误:
redux-persist:需要 config.storage。尝试使用提供的存储引擎之一
import storage from 'redux-persist/lib/storage'
使用AsyncStorage来自@react-native-community/async-storage,
我们得到错误:
[@RNC/AsyncStorage]:NativeModule:AsyncStorage 为空。
使用storage来自redux-persist/lib/storage,
我们得到错误:
console.error:“redux-persist 无法创建同步存储。回退到 noop 存储。”
问:如何解决这个问题而不弹出?谢谢!
redux-persist代码
注意:之前的尝试已被注释掉:
// Chose 1 of the 3 storages
// import { AsyncStorage } from "react-native";
// import AsyncStorage from '@react-native-community/async-storage';
import storage from 'redux-persist/lib/storage'
import { createMigrate, …Run Code Online (Sandbox Code Playgroud) 当前每个值都是在设置前一个值之后设置的,异步调用不会并行执行。如何使这些调用并行执行?
const [index, setIndex] = useState(0);
const [roll, setRollNo] = useState(1);
const [sem, setSemester] = useState(1);
useEffect(() => {
getMyValue();
}, []);
const getMyValue = async () => {
try {
setIndex(JSON.parse(await AsyncStorage.getItem('@branch')) || 0);
setSemester(JSON.parse(await AsyncStorage.getItem('@sem')) || 1);
setRollNo(JSON.parse(await AsyncStorage.getItem('@roll')) || 1);
} catch (e) {
// console.log(e);
}
};
Run Code Online (Sandbox Code Playgroud) 我正在开发一个功能,比如我想在多个屏幕中使用的数据的购物车。此外,想要管理查看购物车和清除购物车项目,但仅限本地。推荐的方式是什么?
1) 异步存储 2) Redux
请帮助我,如果我有误解,请告诉我。
sharedpreferences reactjs react-native react-redux asyncstorage
我正在使用 react-native 和 apollo 客户端,如果我尝试通过存储在 AsyncStorage 中的 jwt 设置标头,它似乎不起作用。
其他不需要标题的解析器工作得很好。我的代码如下。
import { ApolloClient } from "apollo-client";
import { InMemoryCache } from "apollo-cache-inmemory";
import { ApolloLink } from "apollo-link";
import { createHttpLink } from "apollo-link-http";
import AsyncStorage from "@react-native-community/async-storage";
const cache = new InMemoryCache();
const getToken = async () => {
const token = await AsyncStorage.getItem("jwt");
if (token) {
return token;
} else {
return null;
}
};
const httpLink = new createHttpLink({
uri: ""
});
const authLink = new ApolloLink((operation, …Run Code Online (Sandbox Code Playgroud) asyncstorage ×10
react-native ×10
reactjs ×4
async-await ×2
javascript ×2
apollo ×1
expo ×1
facebook ×1
ios ×1
react-redux ×1