【发布时间】:2018-12-08 19:57:36
【问题描述】:
我有这个组件功能
async FetchCards() {
axios.get(settings.default.baseUrl + '/api/cards/get_cards').then((data) => {
var dd = data.data.data;
//this.setState({cards : d});
return(
dd.map(d => {
<Card style={{flex: 0}}>
<CardItem>
<Left>
<Thumbnail source={{uri: d.img}} />
<Body>
<Text>{d.name}</Text>
<Text note>{d.position}</Text>
</Body>
</Left>
<Right>
{d.gender == 'male' && <Icon style={{fontWeight : '900' , fontSize:32 , color : 'darkblue'}} name='ios-male'></Icon>}
{d.gender == 'female' && <Icon style={{fontWeight : '900' , fontSize:32 , color : 'pink'}} name='ios-female'></Icon>}
</Right>
</CardItem>
<CardItem>
<Body>
<Text>
{d.subtitle}
</Text>
</Body>
</CardItem>
</Card>
})
);
}).catch((err) => {
console.log(err);
});
}
当我在这里调用它时
{this.FetchCards()}
它会触发此错误:
Invariant Violation : 对象作为反应子项无效(发现 带有键{_40,_65,_55,_72}) 的对象,如果您打算渲染一个 子元素的集合,请改用数组。
【问题讨论】:
-
看起来您在渲染方法中直接在 JSX 中调用
this.FetchCards。你确定要这样做吗?你不想在回调中或者componentDidMount中这样做吗? -
是的,这是真的 - 我直接在渲染方法中调用它 - 任何简单/快速的解决方案来读取数据然后将其发送到渲染函数
标签: javascript reactjs react-native