Javascript 类组件react native中的映射函数
我是一个新手,我正在尝试迭代一个json文件,希望通过使用map函数来渲染对象。 但是,它不显示对象。有人能告诉我我做错了吗?提前谢谢 这是我的密码:Javascript 类组件react native中的映射函数,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我是一个新手,我正在尝试迭代一个json文件,希望通过使用map函数来渲染对象。 但是,它不显示对象。有人能告诉我我做错了吗?提前谢谢 这是我的密码: import React, { Component } from 'react'; import { View, Text } from 'react-native'; import stations from './test.json'; export default class Playlist extends Component {
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>
)
}
}
将“test.json”文件添加到资产文件夹中,并将其分配给index.js文件,这样当您有更多文件时,可以将其添加到资产下,并从单个位置导出
有关如何使用map()函数的详细信息您需要分散站点
allStations:[…stations]
。此外,导入JSON的方式也不正确。如果我没有正确回答问题,请参阅下面的Plz评论,以便我可以更改我的答案,谢谢
[{
"firstName": "Joe",
"lastName": "Jackson",
"gender": "male",
"age": 28
},
{
"firstName": "Brad",
"lastName": "Brown",
"gender": "male",
"age": 45
}
]