简体   繁体   English

无法在本机反应中打印在 this.state.arr 中声明的数组?

[英]Can't print array declared in this.state.arr in react native?

I have an array called this.state.arr , which I added values to in another function in my app.js file.我有一个名为this.state.arr的数组,我在 app.js 文件的另一个函数中添加了值。 In the render() function I console.log(this.state.arr) the array to show and it shows like this:render()函数中,我console.log(this.state.arr)要显示的数组,它显示如下:

[]
  0: "a"
  1: "b"
  2: "c"
  length: 3

However, I want to be able to access the individual elements of each array.但是,我希望能够访问每个数组的各个元素。 I've tried this.state.arr[0], or const list = this.state.arr.map((e) => {return e;});我试过 this.state.arr[0],或者const list = this.state.arr.map((e) => {return e;}); I've also tried some other commands to access the insides of the this.state.arr array.我还尝试了一些其他命令来访问this.state.arr数组的内部。

But none of those worked.但这些都没有奏效。 I just kept getting null in the console when I tried to access the inner elements of the array.当我尝试访问数组的内部元素时,我只是在控制台中一直为 null。 Any help would be greatly appreciated!任何帮助将不胜感激!

Having null get returned for your attempts to access items in this.state.arr is pretty unusual and makes me think that something else is populating or interfering with your array.为您尝试访问this.state.arr项目而返回null是非常不寻常的,这让我认为其他东西正在填充或干扰您的数组。 When you try to access an item at an index your array that doesn't have a value undefined will get returned.当您尝试访问索引处的项目时,将返回undefined值的数组。 null is typically used to indicate a value/property/attribute is intentionally being set to blank. null通常用于指示有意将值/属性/属性设置为空白。 I'd double check where you are setting / accessing the arr.我会仔细检查您在哪里设置/访问 arr。 Posting that part of the code may help.发布那部分代码可能会有所帮助。

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

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