react-native - React Native:在承诺之外重用承诺参数?
问题描述
我定义了一个获取查询的承诺。我有一个json数组:
fetch('query')
.then(response =>response.json())
.then(json => { blabla})
我想在 promise 之外重用 json 作为函数参数:
render(){
return(
<View onLayout={this.myFonction(json)>
</View>)}
myFunction =(json)=>{
var array = json.names
array.maps(value => {
return (
<Icon
name='heart'
type='emo'
color='red'
onPress={this.onPressIcon(value)} />);
});
}
我得到的只是它是未定义的。甚至可以重用promise参数吗?
解决方案
我们又来了@danaso :)。尝试以这种方式实现您的组件。
您的 fetch 方法将设置组件状态:
fetch('query')
.then(response =>response.json())
.then(json => { this.setState({ array: json.names })})
所以在渲染方法中:
render(){
const { array } = this.state;
return(
<View>
array.map(value => {
return (
<Icon
name='heart'
type='emo'
color='red'
onPress={this.onPressIcon(value)} />
)
})
</View>
)
}
推荐阅读
- microsoft-graph-api - 通过 Graph API 将参考附件发送到电子邮件
- gradle - JacocoTestReport 已跳过
- node.js - 更新 NodeJS 后出现以下错误:无法加载 gRPC 二进制模块
- python - Pandas - 快速读取/写入相同的 csv.. 获取权限错误
- php - 将文件(pdf)附加到重力形式通知
- reactjs - 使用 Prerender.io 如何将爬虫重定向到我们碎片化的站点?
- emacs - 在 Emacs 根项目中保存 TAGS 文件
- arrays - 数组到对象 PHP Codeigniter
- java - 使用 Spring Converter 将 DTO 转换为实体对象,反之亦然
- shell - aws cognito 中的 set-ui-customization 中的 PNG 格式无效