我想获得一些api中的名称,这个api返回一个json,我想让map ()来显示我需要的所有名称,当我在console.warn ()中显示这个map ()时,它会返回完全正确的结果,但是当我试图将结果传递到一个状态并尝试在render ()中使用它时,会出现一个错误
'TypeError:未定义不是一个函数(接近.this.state.fantasyName.map .)错误
我哪里出问题了?
以其他方式尝试更多的空白返回,在console.warn ()中更少
componentDidMount() {
AsyncStorage.getItem('token').then((value) => this.setState({ token:
'Bearer ' + value }));
const uri = "http://192.168.0.149/xpago-
backoffice/api/participant/getparticipant.php"
const requestInfo = {
method: 'POST',
headers: new Headers({
'Content-type': 'application/json',
'Authorization': this.state.token
})
}
fetch(uri, requestInfo)
.then((response) => response.json())
.then((responseJson) => {
this.setState({fantasyName: responseJson})
})
}
renderList() {
const name = this.state.fantasyName.map(nomes => {
const nf = nomes.PAR_FANTASYNAME;
return <Text key={nf}>{nf}</Text>
})
return name;
}
render() {
return (
<View>
<LinearGradient colors={['#41295a', '#2f0743']} style={{
height: 690 }}>
<View style={style.fundo}>
<StatusBar translucent backgroundColor='transparent'
/>
<TouchableHighlight onPress={this.voltar}>
<Image
source={require('../images/voltar.png')}
style={style.voltar}
/>
</TouchableHighlight>
<Text style={style.tit}>Cadastros</Text>
</View>
<ScrollView>
{this.renderList()}
</ScrollView>
<TouchableOpacity style={style.add1} onPress=
{this.cadastro}>
<View>
<Image source={require('../images/add.png')}
style={style.add} />
</View>
</TouchableOpacity>
</LinearGradient>
</View>
)
}我需要在scrollView中显示我的json结果
我是新来的-土生土长的你能帮我吗?
发布于 2019-09-13 14:11:40
您的状态是如何初始化的?无论如何,您应该记住,this.renderList()是在componentDidMount方法之前调用的。在这些情况下,您必须在调用函数时“管理”该函数。就你的情况而言,你可以:
<ScrollView>
{this.state.fantasyName.length && this.renderList()}
</ScrollView>发布于 2019-09-13 19:46:39
从AsyncStorage检索是一个异步调用,我建议使用
let token = await AsyncStorage.getItem('token');其次,请在“获取之后”子句的末尾添加一个.catch,以捕获获取请求中的任何错误。您从API中得到的响应是什么?你能把它发出去吗?我的猜测是,您正在进行的API调用是指向一个不受信任的http源的,为此,我建议使用rn-fetch-blob。
https://stackoverflow.com/questions/57924778
复制相似问题