无法使用React Native异步存储

玛诺·库玛(Manoj Kumar)

我在处理本机异步存储方面遇到困难。大多数情况下,我的代码会从当前执行中跳出,然后转到下一行而不从当前行获取结果。我大多数时间都遇到错误。

我试过这个例子

'use strict';

var React = require('react-native');
var {
  AsyncStorage,
  PickerIOS,
  Text,
  View
} = React;
var PickerItemIOS = PickerIOS.Item;

var STORAGE_KEY = '@AsyncStorageExample:key';
var COLORS = ['red', 'orange', 'yellow', 'green', 'blue'];

var BasicStorageExample = React.createClass({
  componentDidMount() {
    this._loadInitialState().done();
  },

  async _loadInitialState() {
    try {
      var value = await AsyncStorage.getItem(STORAGE_KEY);
      if (value !== null){
        this.setState({selectedValue: value});
        this._appendMessage('Recovered selection from disk: ' + value);
      } else {
        this._appendMessage('Initialized with no selection on disk.');
      }
    } catch (error) {
      this._appendMessage('AsyncStorage error: ' + error.message);
    }
  },

  getInitialState() {
    return {
      selectedValue: COLORS[0],
      messages: [],
    };
  },

  render() {
    var color = this.state.selectedValue;
    return (
      <View>
        <PickerIOS
          selectedValue={color}
          onValueChange={this._onValueChange}>
          {COLORS.map((value) => (
            <PickerItemIOS
              key={value}
              value={value}
              label={value}
            />
          ))}
        </PickerIOS>
        <Text>
          {'Selected: '}
          <Text style={{color}}>
            {this.state.selectedValue}
          </Text>
        </Text>
        <Text>{' '}</Text>
        <Text onPress={this._removeStorage}>
          Press here to remove from storage.
        </Text>
        <Text>{' '}</Text>
        <Text>Messages:</Text>
        {this.state.messages.map((m) => <Text key={m}>{m}</Text>)}
      </View>
    );
  },

  async _onValueChange(selectedValue) {
    this.setState({selectedValue});
    try {
      await AsyncStorage.setItem(STORAGE_KEY, selectedValue);
      this._appendMessage('Saved selection to disk: ' + selectedValue);
    } catch (error) {
      this._appendMessage('AsyncStorage error: ' + error.message);
    }
  },

  async _removeStorage() {
    try {
      await AsyncStorage.removeItem(STORAGE_KEY);
      this._appendMessage('Selection removed from disk.');
    } catch (error) {
      this._appendMessage('AsyncStorage error: ' + error.message);
    }
  },

  _appendMessage(message) {
    this.setState({messages: this.state.messages.concat(message)});
  },
});

exports.title = 'AsyncStorage';
exports.description = 'Asynchronous local disk storage.';
exports.examples = [
  {
    title: 'Basics - getItem, setItem, removeItem',
    render(): ReactElement { return <BasicStorageExample />; }
  },
];

这有效。但是,我的功能无法正常工作。我越来越undefined

洛克希·辛哈(Lokesh Sinha)

看完您的问题后,我首先想到的是“也许”,您特别是ES7的新手async-await请阅读有关ES7中的异步等待功能的信息,该功能将于今年晚些时候(2016年)实现。

您的功能必须async自然。将关键字放在函数之前。并且await必须在调用函数的地方使用。

具体来说,我建议您在开发中使用react-native-store repo。React Native的家伙已经告诉他们使用这个和其他实现,显然同意使用它AsyncStorage有点复杂。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

异步存储无法设置或启动React Native

异步存储React Native的最佳实践

React Native 调试器:异步存储

React Native中的Jest和异步存储

React Native:如何在异步存储中存储对象

React Native-我应该使用异步存储还是cookie?

可以看到异步存储电流限制React Native吗?

React Native - 在 componentWillMount() 之后提取异步存储信息

React Native-如何在异步存储中存储Redux状态

如何在React Native中使用异步数据(AsyncStorage)初始化MobX存储

如何使用React Native将上下文绑定到异步存储

如何在启动时使用异步存储数据呈现React Native组件?

如何在React Native中使用异步存储更改Route.Js中的初始屏幕

无法使用 react-native android 从内部存储读取图像

React Native异步/等待无法正常工作

React Native 异步存储与 UI 不同步?我怎样才能改变这种行为?

如何删除“警告:异步存储已从react-native核心中提取出来...”?

如何在React Native中从异步存储中正确获取数据?

将本机iOS应用程序的数据迁移到React Native应用程序的异步存储

在React Native中将数据从异步存储推送到数组对象时未显示项目?

React Native Fetch异步

无法使用React Native Bluetoothel

React-native iOS,异步存储错误:“无效的键 - 必须至少是一个字符。键:”

需要一些想法在React Native应用程序中实现异步存储以保持用户登录应用程序

如何使用异步存储 Axios

React Native / Javascript 异步等待

React Native:在继续之前无法完成(显然异步)功能

无法在react-native中异步执行sqlite db查询

在React Native中存储