简体   繁体   English

我想在本机反应中显示选定的数字

[英]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.

相关问题 我想在反应原生 dateTimePicker 中显示选定的日期和时间 - I want to show Selected Date and time in react native dateTimePicker 我想根据 state 在本机反应中隐藏/显示按钮组件 - I want to hide/show button component in react native depending on state 如何在 React Native 中显示选定的时间 - How can I show the selected time in React Native 我想通过react-native-vector图标和react-native-router-flux在rightButtonImage上显示Icon - I want to show Icon on rightButtonImage by react-native-vector icon and react-native-router-flux 如何在React Native中显示不同的所选项目 - How to show selected item different in React Native 我想在响应本机中将平面列表视图的选定值设置到输入字段中 - I want to set selected value of flat list view into the input field in react native 当我触摸我的文本输入时,我希望键盘根本不显示-React-Native - i want keyboard not to show up at all when i touch my text input-React-Native 我在我的 React Native 应用程序中使用 react-native-camera 进行视频录制。 我想用它显示计时器。 我是 React Native 的新手 - I am using react-native-camera for video recording in my React Native App. I want to show timer with it. I am new to React Native 使用 resizeMode=cover 在 react native 中显示图像,但我想切掉底部而不是顶部 - Show an image in react native with resizeMode=cover but I want to chop off the bottom not top 我想访问对象反应本机数组 - I want to access an array of object react native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM