【问题标题】:React Native's similar to React's .map?React Native 类似于 React .map?
【发布时间】:2016-01-04 07:52:24
【问题描述】:

在 React 中,从 props 中循环遍历数组中的对象时,我使用了 .map。这似乎不适用于 React Native。是不是我写错了?

反应原生道具:

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来自于在父组件&lt;UsersAdded passProps={this.props}/&gt;调用这个组件

console.log 记录用户名和索引号。但是后来我从父组件那里得到一个错误,说A valid ReactComponent must be returned. You may have returned undefined, an array or some other invalid object. 但是,取出.map 函数并放入一个简单的&lt;Text&gt;Test&lt;/Text&gt; 可以正确呈现。

React Native 中的方法是什么?

【问题讨论】:

  • return this.props.passProps.users.map(function (user, i). 输入'return'关键字。希望它有效。

标签: javascript arrays react-native


【解决方案1】:

有两点你应该更正:

  1. render 方法中忘记了 return 语句

  2. Render 不应返回元素数组。将映射的数组封装到一个将被返回的&lt;View /&gt; 元素中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-14
    • 2021-09-05
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 2019-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多