arrays - 如何正确地将数组映射到 <Text> (React native)

标签 arrays react-native

我有一个数组需要映射到 <Text> 中.这就是我在 render() 中得到的

categories.map(category => <Text>{category.testHeader}</Text>

但是它不打印任何东西。我猜 <Text>需要在 render()正确的?所以我尝试将它添加到要在 render 中调用的函数中.像这样:

function myfunc() {
    return categories.map(category => <Text>{category.testHeader}</Text>)
}

然后在render() :

<View>
    {myfunc()}
</View>

但随后编译器说“无法读取未定义的属性‘map’”。所以提示告诉我写:

function myfunc() {
    if (this.props.data) {
        return categories.map(category => <Text>{category.testHeader}</Text>)
    }
}

但是现在编译器告诉我 data未定义。不知道在这里做什么...:/

最佳答案

你可以这样使用:

var categories = [{ id: 0, text: 'hasan' }, 
    { id: 1, text: 'erkan' },
    { id: 2, text: 'veli' }];

export default class App extends Component {
renderCategories() {
    return categories.map((item, index) => <Text key={index}>{item.text}</Text>);
}
render() {
    return (
        <View style={styles.container}>
            {this.renderCategories()}
        </View>
    );
}

关于arrays - 如何正确地将数组映射到 <Text> (React native),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49728218/

相关文章:

arrays - c# 单参数函数数组,每个函数具有不同类型的参数

C# - 获取字符串中的整数字节数组

javascript - 比较多个 JS 数组的值

react-native - 如何在 React Native 中实现付费去除广告(iOS 和 Android)

javascript - TypeError : Object. 条目要求输入参数在 React Native 中不为空或未定义

arrays - "Value of type ' AnyObject ' has no member."..尝试在 Swift 中为数组中的对象赋值时发生错误

javascript - 动态填充 javascript 对象

reactjs - 如何将 setTimeout 添加到使用 redux 的 fetch Promise 中?

android - 找不到变量 FlatList 或 ListView

react-native - React Native STUCK 启动打包器