yog*_*air 6 android react-native
如何以本机存储/保存数据.就像我们在android中使用共享偏好一样,有任何反应原生的解决方案.我是反应性的新手.
请添加示例示例
Har*_*iks 11
您可以使用React Native AsyncStorage将数据存储到设备的本地存储.
import { AsyncStorage } from 'react-native'
Run Code Online (Sandbox Code Playgroud)
用它来保存数据
AsyncStorage.setItem('key', 'value');
Run Code Online (Sandbox Code Playgroud)
AsyncStorage仅接受值作为字符串,因此您可能需要stringify()在将值设置为AsyncStorage之前使用
并检索数据使用
AsyncStorage.getItem('your key');
Run Code Online (Sandbox Code Playgroud)
示例代码:
const KEY = 'USER_DATA'
let keyValue = { name: yogi }
AsyncStorage.setItem(KEY,keyValue);
AsyncStorage.getItem(KEY).then(asyncStorageRes => {
console.log(JSON.parse(asyncStorageRes))
});
Run Code Online (Sandbox Code Playgroud)
React Native也有共享的首选项,这是npm命令来获取它:npm install react-native-shared-preferences --save。您可以在这里获取完整的详细信息(如何在react项目中进行设置):https : //www.npmjs.com/package/react-native-shared-preferences