我正在测试一个嵌套在Navigator中的FlatList,并尝试了解它的工作原理。下面的代码可以正常工作:
<FlatList
data={[{key: 'a'}, {key: 'b'}]}
renderItem={({item}) => <Text>{item.key}</Text>}
horizontal={true}
/>
但是这个不行:
<FlatList
data={[{key: 'a'}, {key: 'b'}]}
renderItem={({item}) => <TextComp data={item}/>}
horizontal={true}
/>
TextComp是一个组件,用于显示item.key。当单独测试时,它的功能是正常的。以下是代码:
<View>
<Text>{this.props.data.key}</Text>
</View>
我也尝试在两个组件周围画边框,似乎FlatList正在渲染,但项目没有被渲染。
我正在我的Android设备上进行测试。
更新:我向TextComp组件添加了console.log(this.props)语句,并且它正确地显示了props,因此正确的数据已从FlatList传递到TextComp,但TextComp未被渲染。
TextComp
的代码吗? - Jose VfTextComp
组件中添加了一个返回语句吗?例如:const TextComp = () => { return(<Text>...</Text>) }
- RayTextComp
的代码时,我是在要求完整的代码,这种情况下组件内部的所有内容(如何处理 props 和返回组件的方式)非常重要,仅显示<View><Text>{props.text}</Text></View>
没有任何用处,所以如果可能的话,请分享组件的完整代码,以便更好地了解可能出现的问题。 - Jose Vf