React native 反应本机更新平面列表项
我想在单击某个特定的平面列表行时更新该行,我如何才能做到这一点?我找到的解决方案都使用extraData={this.state},这在我的代码中不起作用React native 反应本机更新平面列表项,react-native,React Native,我想在单击某个特定的平面列表行时更新该行,我如何才能做到这一点?我找到的解决方案都使用extraData={this.state},这在我的代码中不起作用 const [records,setRecords] = useState([]); //add initial data to records ... return ( <FlatList data={records} keyExtractor={item => item.id} renderItem={
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>
}
/>)
要更新flatlist项,您只需更新它从中渲染的数据,为此,在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>
}
/>)