AsyncStorage说&undecile不是一个对象'

时间:2016-11-02 02:25:02

标签: react-native asyncstorage

我正在制作购物清单,并希望将已检查的商品保存在本地存储空间中。但是,设置AsyncStorage对我来说并不是说undefined不是对象

这是我的代码:

import React from 'react';
import { StyleSheet, Dimensions } from 'react-native';
import { Container, Content, List, ListItem, Text, CheckBox, AsyncStorage } from 'native-base';

export default class ShoppingListItem extends React.Component {
  constructor(props) {
    super(props);
    this.saveCheck = this.saveCheck.bind(this)
    this.state = {
      checked: false
    }
  }

  render() {
    return (
      <ListItem style={styles.table}>
        <CheckBox 
          checked={this.state.checked}
          onPress={this.saveCheck}/>
        <Text>{this.props.item[0]} {this.props.item[1]} {this.props.item[2]}</Text>
      </ListItem>
    );
  }

  // componentWillMount () {
  //   AsyncStorage.getItem(this.props.key).then((value) => {
  //      this.setState({checked: value});
  //   }).done();
  // }

  saveCheck () {
    console.log(this.state.checked)
    this.setState({checked: !this.state.checked})
    AsyncStorage.setItem('checked', JSON.stringify(this.state.checked))
  }
}

1 个答案:

答案 0 :(得分:3)

问题在于行this.props.key

请参阅react-native

抛出的此警告

enter image description here

因此,将道具'key'更改为其他名称,然后一切都应按预期工作。

还从'react-native'

导入AsyncStorage

更改

import { StyleSheet, Dimensions } from 'react-native';

import { StyleSheet, Dimensions, AsyncStorage} from 'react-native';

并从'native-base'中删除AsyncStorage