【发布时间】:2020-11-30 14:39:20
【问题描述】:
我是原生反应的新手,所以请善待!我正在尝试使用 JSON 填充平面列表。
下面是我的 JSON 数据
{
"h1":{
"baseprice":899,
"description":"Upto Waist length Hair",
"imageUrl":"https://i.imgur.com/0IgYzAv.jpg'",
"price":799,
"time":"25 min",
"title":"Nourishing Hair Spa",
"type":"Service"
},
"h2":{
"baseprice":899,
"description":"Touch Up of length less than 4 inches",
"imageUrl":"https://i.imgur.com/q7ts4PZ.jpg",
"price":799,
"time":"45 min",
"title":"INOA Root Touch Up",
"type":"Service"
}
}
这是我用来将 JSON 数据推送到我的 Flatlist 的代码
export default class Office extends Component {
constructor(props) {
super(props);
this.state = {
isLoading: true,
dataSource: [],
};
}
componentDidMount() {
return fetch("https://stylmate1.firebaseio.com/hair.json")
.then((response) => response.json())
.then((responseJson) => {
this.setState({
isLoading: false,
dataSource: responseJson,
});
console.log(dataSource);
})
.catch((error) => {
console.error(error);
});
}
render() {
if (this.state.isLoading) {
return (
<View style={{ flex: 1, paddingTop: 20 }}>
<ActivityIndicator />
</View>
);
}
return (
<View style={styles.container}>
<FlatList
data={this.state.dataSource}
renderItem={(item) => <Text>{item.title}</Text>}
/>
</View>
);
}
}
我一刷新应用程序就会收到错误消息 找不到变量:数据源 但是如果我 console.log(responseJson);然后我得到完整的 JSON 对象。
我不知道我在这里做错了什么。请帮我解决这个问题。
【问题讨论】:
-
h1、h2 对你来说重要吗?
标签: arrays json react-native react-native-flatlist