tgaribal
tgaribal

Reputation: 11

AsyncStorage saying 'undefined is not an object'

I am working on a shopping check list and want to save the checked items in local storage. However, setting up AsyncStorage hasnt been working for me saying undefined is not an object

Here is my code:

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))
  }
}

Upvotes: 1

Views: 5393

Answers (1)

Jickson
Jickson

Reputation: 5193

The issue is with the line this.props.key

See this warning thrown by react-native

enter image description here

So change your props 'key' to some other name, then everything should work as expected.

Also import AsyncStorage from 'react-native'

Change

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

to

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

and remove AsyncStorage from 'native-base'

Upvotes: 5

Related Questions