简体   繁体   中英

React console.log not showing array values

Console it printing Array [] instead of values. I have checked the code but figure it out. Why doesn't console.log show me array values when i click button?

import React from "react";
import { StyleSheet, View, FlatList } from "react-native";

import DataManager from "../config/DataManager";

const getMemories = () => {
  let commonData = DataManager.getInstance();
  let user = commonData.getUserID();
  return commonData.getMemories(user);
  
}

function MemoriesScreen(props) {
  const memoryList = getMemories();
  console.log(memoryList);

  return (
      <AppScreen style={styles.container}>

          <FlatList 
              data={memoryList}
              keyExtractor = {memory => memory.memoryid.toString()}
              renderItem = {({item}) => 
                      <AppCard
                          title={item.title}
                          subtitle={item.subtitle}
                          image={item.image}
                          category={item.category}
                      />}
              />
      </AppScreen>
  );
}

const styles = StyleSheet.create({
  container:{
      backgroundColor:AppColors.otherColor,
      flex:1,
      marginTop:0,

  },
})
export default MemoriesScreen;

this the DataManger js screen

export default class DataManager {
  static myInstance = null;
  userID = "";

 memories = [
    {
      userid: "user1",
      memoryid: 1,
      title: "Memories of School days",
      subtitle: "Created on 3rd of January, 2005",
      image: require("../assets/School-days.jpeg"),
      category: "Childhood",
    },
    {
      userid: "user1",
      memoryid: 2,
      title: "Memories of School days",
      subtitle: "Created on 3rd of January, 2005",
      image: require("../assets/School-days.jpeg"),
      category: "Childhood",
    },
    {
      userid: "user2",
      memoryid: 1,
      title: "Memories of School days",
      subtitle: "Created on 3rd of January, 2005",
      image: require("../assets/School-days.jpeg"),
      category: "Childhood",
    }
  
  ];

  static getInstance() {
    if (DataManager.myInstance == null) {
      DataManager.myInstance = new DataManager();
    }
    return this.myInstance;
  }

  getUserID() {
    return this.userID;
  }

  setUserID(id) {
    this.userID = id;
  }
  getMemories(id) {
    return this.memories.filter((memory) => memory.userid === id);
  }
  addMemory(memory){
    this.memories.push(memory);
  }
}

Your array can be empty. Push something before log and check your array like this.

memoryList.push("test memory"); 
console.log(memoryList);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM