我有一个数组需要映射到 <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/