简体   繁体   English

React console.log 不显示数组值

[英]React console.log not showing array values

Console it printing Array [] instead of values.控制它打印 Array [] 而不是值。 I have checked the code but figure it out.我已经检查了代码,但弄明白了。 Why doesn't console.log show me array values when i click button?为什么当我单击按钮时 console.log 不显示数组值?

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这是 DataManger js 屏幕

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 反应console.log不显示状态数组值 - React console.log not showing state array values 尽管在 console.log 中显示,但 React Array 未在 .map 中显示数据 - React Array not showing data in .map despite showing in console.log console.log React 中选中复选框值的数组 - console.log an array of checked checkbox values in React Console.log 在 Chrome 控制台中未显示任何内容。 (反应) - Console.log not showing anything in Chrome Console. (React) Console.log 显示正确的值,但映射值时该值未在 React/NextJS 中呈现 - Console.log is showing correct value, but the value is not rendering in React/NextJS when mapping values 什么是react组件显示的render方法中的console.log? - What is console.log in the render method of a react component showing? console.log this.state 没有在反应中显示当前状态 - console.log this.state is not showing the current state in react React Redux,来自 reducer 的值在 console.log 上显示为 undefined - React Redux , value from reducer showing undefined on console.log API响应显示在console.log中,但未显示在react页面上 - API response showing in console.log but not on react page React &amp; Axios:无法在 UI 中呈现数组值,但我可以控制台记录它们 - React & Axios: Unable to render array values in UI but I can console.log them
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM