在 React 中,当从 props 循环访问数组内的对象时,我使用了 .map
。这在 React Native 中似乎不起作用。也许我写错了?
React Native Prop :
var appObject = {
users:[
{name:'Jack'},
{name:'Jill'}
]
}
在 React Native 组件中:
render(){
this.props.passProps.users.map(function (user, i){
console.log('user: ',user.userName,'i',i);
return (
<Text key={i}>{user.userName}</Text>
);
});
}
passProps来自于在父组件中调用该组件<UsersAdded passProps={this.props}/>
console.log
记录用户名和索引号。但随后我从父组件收到一条错误消息 A valid ReactComponent must be returned. You may have returned undefined, an array or some other invalid object.
但是,取出 .map 函数并放入一个简单的 <Text>Test</Text>
正确渲染。
React Native 中的方法是什么?
最佳答案
有两件事您应该更正:
忘记了
return
render
中的声明方法渲染不应返回元素数组。将映射的数组封装成一个
<View />
将返回的元素。
关于javascript - React Native 和 React 的 .map 类似吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34586792/