javascript - 如何让平面列表只呈现一次?
问题描述
我试图只打印一次 ComponentTwo Flatlist ,但相反,我得到了结果image1但相反,我需要它看起来像这个图像 2。我附上了一个小吃链接,里面有代码。
将产生与图像Expo Snack Link中相同结果的代码
解决方案
工作示例:世博小吃
这是解决此问题的方法,首先将index
值传递给ComponentOne
fromApp.js
const App = () => {
return (
<SafeAreaView style={styles.container}>
<FlatList
data={DATA}
renderItem={({item, index}) => <ComponentOne name={item.title} index={index}/>}
keyExtractor={(item) => item.id}
/>
</SafeAreaView>
);
};
现在使用该道具值ComponentTwo
有条件地渲染,ComponentOne
如下所示:
//...const
ComponentOne = (props: ComponentOneProps) => {
return (
<View style={{ margin: 15, backgroundColor: 'yellow' }}>
<FlatList
data={recent}
renderItem={({ item }) => {
console.log("hello")
// @ts-ignore
return props.index == 0?<ComponentTwo name={item.name} />:null;
}}
keyExtractor={(item) => item.idd}
/>
//...
推荐阅读
- podman - 无法使用 podman 推送到本地重新映射的注册表
- c - 为什么等待后我不能关闭管道?
- javascript - 无法使用 ejs 路由参数和猫鼬呈现页面
- java - Maven 多模块项目相同的依赖项被最新版本覆盖
- sql - 从 SQL 查询返回不匹配的值
- javascript - Flatlist 和数据映射未显示数据
- c# - 如何在 MVC 中使用按钮调用方法
- go - Bigquery 实体标签
- python - 如何从一个用户停止多个相同的命令?(不和谐机器人)
- php - Laravel local.ERROR: array_merge(): Expected parameter 2 to be an array, string given