R
R
Rufix2020-03-31 15:22:50
JavaScript
Rufix, 2020-03-31 15:22:50

How to output data from AsyncStorage + state (TypeError: undefined is not a function (near '..._this.state.noteList.map...'))?

Hello. The question is in the title. Source code here or below: https://pastebin.com/0JnA6D5b

import React from "react";
import {
  StyleSheet,
  View,
  ScrollView,
  Button,
  Text,
  AsyncStorage
} from "react-native";
 
export default class Home extends React.Component {
  static navigationOptions = {
    title: "Название приложения"
  };
 
  constructor(props) {
    super(props);
    this.state = {
      date: "",
      noteList: []
    };
  }
 
  displayNoteList = () => {
    return this.state.noteList.map(note => {
      return (
        <View>
          <Text>{note.id}</Text>
        </View>
      )
    })
  }
 
componentDidMount() {
 
    AsyncStorage.getItem('noteList', (error, result) => {
      if (!error) {
        if (result !== null) {
          this.setState({ noteList: result })
        }
      }
    })
  }
 
render() {
    return (
        <View>
            { this.displayNoteList() }
        </View>
    );
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
real2210, 2020-04-01
@real2210

Since AsyncStorage is asynchronous, most likely your displayNoteList method is trying to render something that note.id doesn't have.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question