joh*_*son 0 arrays react-native
我有一个需要在<Text>. 这是我的render()
categories.map(category => <Text>{category.testHeader}</Text>
Run Code Online (Sandbox Code Playgroud)
但它不打印任何东西。我想<Text>需要在render()对吗?所以我尝试将它添加到要调用的函数中render。像这样:
function myfunc() {
return categories.map(category => <Text>{category.testHeader}</Text>)
}
Run Code Online (Sandbox Code Playgroud)
然后在render():
<View>
{myfunc()}
</View>
Run Code Online (Sandbox Code Playgroud)
但是随后编译器说'无法读取未定义的属性'map'。SO提示告诉我写:
function myfunc() {
if (this.props.data) {
return categories.map(category => <Text>{category.testHeader}</Text>)
}
}
Run Code Online (Sandbox Code Playgroud)
但是现在编译器告诉我这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>
);
}
Run Code Online (Sandbox Code Playgroud)
}
| 归档时间: |
|
| 查看次数: |
5869 次 |
| 最近记录: |