react-native - React Native 更新一个平面列表项
问题描述
我想在单击时更新特定的 Flatlist 行,我该怎么做?我找到的解决方案都是 using extraData={this.state}
,这在我的代码中不起作用。
const [records,setRecords] = useState([]);
//add initial data to records
...
return (
<FlatList
data={records}
keyExtractor={item => item.id}
renderItem={itemData =>
<RecordItem
onSelect={() => {
props.navigation.navigate({...})
}
}
onApply={()=>{
// in my RecordItem these is a button to call onApply(),
and I want to update the Flatlist when I click this button
var res = applyHandler();
return res;
}}
>
</RecordItem>
}
/>)
解决方案
要更新您的平面列表项,您只需更新它正在呈现的数据,为此,在您的onApply
函数中您知道可以更新记录索引的项索引,例如:
const [records,setRecords] = useState([]);
//add initial data to records
...
return (
<FlatList
data={records}
keyExtractor={item => item.id}
renderItem={itemData =>
<RecordItem
onSelect={() => {
props.navigation.navigate({...})
}
}
onApply={()=>{
// here you will get reference to record item index,
const itemIndex = itemData.index;
const modRecords = [...records];
modRecords[itemIndex].title = 'NEW TITLE';
// assuming title is what you want to change
// now just update the records using setRecords
setRecords(modRecords);
}}
>
</RecordItem>
}
/>)
推荐阅读
- css - SVG 圆环图中段之间的间隙
- jquery - 我如何确定每个表的最高数字是多少
- javascript - 如何使用 XMLHttpRequest 在另一个 HTML 文件中显示 html 文件?
- javascript - 无法使用从 chlid 组件到父组件的数据?
- mysql - 尝试统计该在线用户正在处理的订单
- javascript - 在配置 react-native-maps 中获取 "supportLibVersion" 、 "playServicesVersion" 和 "androidMapsUtilsVersion" 值
- networking - 如何使用 calico 网络从节点访问 Kubernetes 服务
- javascript - 异步执行的函数的返回值
- python - 如何创建一个嵌套的理解列表来计算excel文件列表中的工作表数?
- r - 如何修复以下“hclust(d, method = hclustfun) 中的错误:外部函数调用中的 NA/NaN/Inf (arg 11)”