简体   繁体   English

Map function 在 class 组件反应原生

[英]Map function in class component react native

I am new to React and I'm trying to iterate through a json file, want to render the objects by using the map function. However it doesn't display the objects.我是 React 的新手,我正在尝试遍历 json 文件,想通过使用 map function 来呈现对象。但是它不显示对象。 Can someone please tell me want I am doing wrong?有人可以告诉我我做错了吗? Thanks in advance.提前致谢。

Here is my code:这是我的代码:

import React, { Component } from 'react';
import { View, Text } from 'react-native';
import stations from './test.json';



export default class Playlist extends Component {
    constructor(props) {
        super(props);
        this.state = {
            allStations: [stations],

        };

    }



    render() {
        return (

            <View>
                {this.state.allStations.map((item, index) => (
                    (
                        <View>
                            <Text>{item.firstName}</Text>
                        </View>
                    )
                ))}

            </View>




        )
    }
}

This is my json file:这是我的 json 文件:

[{
    "firstName": "Joe",
    "lastName": "Jackson",
    "gender": "male",
    "age": 28

   
 },
 {
    "firstName": "Brad",
    "lastName": "Brown",
    "gender": "male",
    "age": 45
   
   
 }
]

I am new to React and I'm trying to iterate through a json file, want to render the objects by using the map function.我是 React 新手,我正在尝试遍历 json 文件,想使用 map function 来渲染对象。 However it doesn't display the objects.但是它不显示对象。 Can someone please tell me want I am doing wrong?有人可以告诉我想要我做错了吗? Thanks in advance.提前致谢。

Here is my code:这是我的代码:

import React, { Component } from 'react';
import { View, Text } from 'react-native';
import stations from './test.json';



export default class Playlist extends Component {
    constructor(props) {
        super(props);
        this.state = {
            allStations: [stations],

        };

    }



    render() {
        return (

            <View>
                {this.state.allStations.map((item, index) => (
                    (
                        <View>
                            <Text>{item.firstName}</Text>
                        </View>
                    )
                ))}

            </View>




        )
    }
}

This is my json file:这是我的 json 文件:

[{
    "firstName": "Joe",
    "lastName": "Jackson",
    "gender": "male",
    "age": 28

   
 },
 {
    "firstName": "Brad",
    "lastName": "Brown",
    "gender": "male",
    "age": 45
   
   
 }
]

Add your "test.json" file inside an assets folder and assign it to an index.js file, so when you have more files you can add them under assets and export them from a single location在资产文件夹中添加“test.json”文件并将其分配给 index.js 文件,因此当您有更多文件时,您可以将它们添加到资产下并从一个位置导出它们

Have a look into this code, Thankyou看看这段代码,谢谢

For more information on how to use the map() function https://www.pluralsight.com/guides/how-to-use-the-map()-function-to-export-javascript-in-react有关如何使用 map() 的更多信息 function https://www.pluralsight.com/guides/how-to-use-the-map()-function-to-export-javascript-in-react

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

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