[英]i want to show selected numbers in react native
I have created a list of numbers and i want to show selected items in a different view but my codes show only the pressed item.我创建了一个数字列表,我想在不同的视图中显示选定的项目,但我的代码只显示按下的项目。 i want it to like 1 2 3.. of the numbers in the view if onPress is fired
如果 onPress 被解雇,我希望它喜欢视图中的 1 2 3.. 数字
import React,{useState} from "react";
import { FlatList, ScrollView, SafeAreaView, View, StyleSheet, Text, TouchableOpacity } from "react-native";
here is what display the list of numbers这是显示数字列表的内容
const Item = ({ item, onPress, onLongPress, backgroundColor, textColor }) => {
return (
<TouchableOpacity onPress={onPress}
style={[styles.item, backgroundColor]}>
<Text style={[styles.title, textColor]}>{item.NO}</Text>
</TouchableOpacity>
);
}
FLATLIST COMPONENT shows the render items FLATLIST COMPONENT 显示渲染项
export const FlatListComponent = (item,textColor) => {
const navigation = useNavigation();
const [numbers, setNumbers] = useState([
{ id: 1, NO: '1' }, { id: 2, NO: '2' }, { id: 3, NO: '3' }, { id: 4, NO: '4' }, { id: 5, NO: '5' },
{ id: 6, NO: '6' }, { id: 7, NO: '7' }, { id: 8, NO: '8' }, { id: 9, NO: '9' }, { id: 10, NO: '10' },
{ id: 11, NO: '11' }, { id: 12, NO: '12' }, { id: 13, NO: '13' }, { id: 14, NO: '14' }, { id: 15, NO: '15' },
{ id: 16, NO: '16' }, { id: 17, NO: '17' }, { id: 18, NO: '18' }, { id: 19, NO: '19' }, { id: 20, NO: '20' },
]);
const [selectedItems, setSelectedItems] = useState([]);
const RenderItem = ({ item }) => {
const backgroundColor = item.NO === selectedItems? "#6e3b6e" : "#f9c2ff";
const color = item.NO === selectedItems ? 'white' : 'black';
return (
<Item
item={item}
backgroundColor={{ backgroundColor }}
textColor={{ color }}
onPress={() => setSelectedItems(item.NO)}
/>
);
};
return (
<SafeAreaView style={styles.container}>
<ScrollView contentContainerStyle={{ flexDirection: "row", flexWrap: "wrap" }}>
{numbers.map((item, index) => {
return (
<View style={{ width: '20%', flexDirection: "row" }}>
<RenderItem
keyExtractor={item.id}
item={item}
extraData={selectedItems}
/>
</View>
)
}
)}
<View >
<Text>{selectedItems}</Text></View>
</ScrollView>
</SafeAreaView>
);
};
const [selectedList , setSelectedList] = useState([])
push the selected item in selectedList Array =>在 selectedList 数组中推送所选项目 =>
if (!selectedList?.includes(item.id)){
selectedList.push(item.no);
setSelectedList(selectedList)
}
and view the selectedList.并查看 selectedList。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.