gpt4 book ai didi

react-native - TypeError : this. state.data.map 不是函数

转载 作者:行者123 更新时间:2023-12-02 00:34:17 30 4
gpt4 key购买 nike

我是 React-native 的新手,我试图让我的代码命中特定的 API,然后显示返回的 JSON 中的一条信息,但出于某种原因,从该 api 返回的 JSON即使它适用于其他 api,也不起作用。我已经挣扎了几天了,我只是很困惑。谁能告诉我我做错了什么?

import React, { Component } from 'react';
import { FlatList, StyleSheet, Text, View} from 'react-native';
import axios from 'axios';

export default class App extends Component {
state = { data: [] };

componentDidMount() {
axios.get('https://dvlasearch.appspot.com/AccountInfo?apikey=DvlaSearchDemoAccount')
.then(response => this.setState({ data: response.data }));
}

renderData() {
return this.state.data.map(data => <Text>{data.client}</Text>);
}

render() {
console.log(this.state);

return (
<View>
{this.renderData()}
</View>
);
}
}
const styles = StyleSheet.create({
container: {
marginTop: 15,
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
}
});

最佳答案

constructor(props) 中为您的状态赋值

constructor(props)
{
this.state = {
data: []
}
}

关于react-native - TypeError : this. state.data.map 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50407102/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com