我正在尝试在 FlatList 中选择单个项目。所需的功能是单击取消关注,并且已单击的特定项目从“取消关注”更改为“关注”。到目前为止,我已经设法完成了这个功能,但问题是当我手动重新渲染组件时进行了更改。
这是代码:
function FolloweringScreens({
All my props here
}) {
const [refresh, setRefresh] = useState(false);
const onClickItem = (item, index) => {
return item.isFollowed
? (item.isFollowed = false)
: (item.isFollowed = true);
};
return (
<>
<FlatList
scrollEnabled={true}
onEndReachedThreshold={0}
refreshing={refresh}
onRefresh={handleFetch}
onEndReached={noMoreData || loadingMore ? null : handleMoreData}
data={data}
style={{marginTop: height * 0.07}}
keyExtractor={(i, index) => index.toString()}
renderItem={({item, index}) => {
return (
<>
<TouchableHighlight
style={styles}
underlayColor="transparent"
onPress={() => navigation.navigate('Profile page', {item})}>
<View
style={styles}>
{User avatar and users Name components here}
{screen === 'Followers' ? (
<View>
<TouchableHighlight
underlayColor="transparent"
style={{marginLeft: 0}}
onPress={() => {
item.isFollowing
? onClickItem(item, index)
: onClickItem(item, index);
}}>
{item.isFollowing ? (
<Text>Unfollow</Text>
) : (
<Text>Follow</Text>
)}
</TouchableHighlight>
</View>
) : (
<View>
<TouchableHighlight
underlayColor="transparent"
style={{marginLeft: 0}}
onPress={() => {
item.isFollowed
? onClickItem(item, index)
: onClickItem(item, index); <<<<-- This is where I am selecting the item
}}>
{item.isFollowed ? (
<Text>Unfollow</Text>
) : (
<Text>Follow</Text>
)}
</TouchableHighlight>
</View>
)}
</View>
</TouchableHighlight>
</>
);
}}
/>
</>
);
}
export default FolloweringScreens;