{item.item.name}?-6ren"> {item.item.name}?-这里我试图在 FlatList 中显示一个名为“posts”的数组。 render() { console.log(this.props.posts); return ( -6ren">
gpt4 book ai didi

javascript - React Native 的 FlatList prop renderItem 需要额外的 "item"(item) => {item.item.name}?

转载 作者:行者123 更新时间:2023-11-29 18:46:14 26 4
gpt4 key购买 nike

这里我试图在 FlatList 中显示一个名为“posts”的数组。

render() {
console.log(this.props.posts);
return (
<View style={this.styles.container}>
<FlatList
data={this.props.posts}
renderItem={(item) => <Text> {item.name} </Text>}
/>
</View>
);
}

正如在此控制台日志中所见,posts 数组已正确填充。 enter image description here

但是上面的代码并没有显示FlatList中的任何数据。

enter image description here

但是,在 renderItem 中,如果我添加一个额外的“item”属性,它就会起作用。

enter image description here

renderItem={(item) => <Text> {item.item.name} </Text>}

这种行为的原因是什么。

最佳答案

ReactNative 的 FlatList 输入不是 item ,但是一个包含 3 个参数的对象:item对于实际数据,index对于索引和 separators对象来自定义您的项目组件。您所做的是将该对象命名为 item , 并得到实际的 item来自对象。

为避免混淆,请考虑使用 ES6 速记:

renderItem={({ item, index }) => <Text> {item.name} </Text>}

关于javascript - React Native 的 FlatList prop renderItem 需要额外的 "item"(item) => {item.item.name}?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53776318/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com