1

I'm developing an application using react-native in this application I declare an array to hold the details of the items. Using this array I managed to get data of all the objects inside of the array to a flat list but Instead of displaying all the objects in the array, I want to get certain items and their details to the FlatList using a dropdown picker and a button press one at a time.

This is the FlatList template that I used to get the array items to the FlatList

    const ListItem = ({ item, onSubtract, onAdd }) => (
      <View
        style={{
          flexDirection: 'row',
          justifyContent: 'space-between',
          paddingVertical: 12,
          paddingHorizontal: 10,
          borderBottomWidth: 1,
          borderColor: "#ddd",
        }}>
    
        <View style={{ flexDirection: 'row',}}>
          <Text style={styles.cardText} >{item.name} </Text>
        </View>
    
        <View style={{ flexDirection: 'row',}}>
    
        <TouchableOpacity style={styles.icons}  onPress={onSubtract} >
        <FontAwesomeIcon style={styles.startIcon} icon={faMinusSquare} size={25} />
        </TouchableOpacity>
    
          <Text style={{color: '#fff',
                        fontSize: 16,
                        marginLeft: 5,
                        marginRight: 5,
                        fontFamily: 'Poppins-Medium',}} >{item.quantity}</Text>
          
          <TouchableOpacity style={styles.icons}  onPress={onSubtract} >
          <FontAwesomeIcon style={styles.startIcon} icon={faPlusSquare} size={25}  onPress={onAdd} />
          </TouchableOpacity>
          
    
        </View>
        
        <Text style={styles.cardText} > {Math.round((item.price * item.quantity) * 100) / 100} </Text>
        <Text style={styles.cardText}> {Math.round(((item.price * item.quantity) * 0.05) * 100) / 100} </Text>
      </View>
    );

This is the declared array and the functions

    const [quantity, setQuantity] = useState(0);
      const [selectedProduct, setSelectedProduct] = useState();
      const [products, setProducts] = React.useState([
        { _id: 1, name: 'Item 1', price: 300.00, tax: 0, quantity: quantity },
        { _id: 2, name: 'Item 2', price: 29.00, tax: 0, quantity: quantity },
        { _id: 3, name: 'Item 3', price: 200.00, tax: 0, quantity: quantity },
      ]);
    
    
      const onSubtract = (item, index) => {
        const nextProducts = [...products];
        nextProducts[index].quantity -= 1;
        setProducts(nextProducts);
      };
    
      const onAdd = (item, index) => {
        const nextProducts = [...products];
        nextProducts[index].quantity += 1;
        setProducts(nextProducts);
      };
    
      let totalQuantity = 0;
      let totalPrice = 0;
      let totalTax = 0;
      let totalPriceOfTheInvoice = 0;
      products.forEach((item) => {
        totalQuantity += item.quantity;
        totalPrice += item.quantity * item.price;
        totalTax += item.quantity * (item.price * 0.05);
        totalPriceOfTheInvoice = totalPrice + totalTax;
      });

return (
    <SafeAreaView style={{ flex: 1 }}>

      <View >
        <Picker
          style={{ color: '#fff' }}
          selectedValue={selectedProduct}
          onValueChange={setSelectedProduct}
        >
          {products.map(item => (
            <Picker.Item label={item.name} value={item._id} />
          ))}
        </Picker>
      </View>

      <Button title="getDetails" onPress={onSendSelectDetailsToFlatList} />


      <FlatList
        data={products}
        renderItem={({ item, index }) => (
          <ListItem
            item={item}
            onSubtract={() => onSubtract(item, index)}
            onAdd={() => onAdd(item, index)}
          />
        )}
        keyExtractor={(item) => item._id}
      />

    </SafeAreaView>
      );
    };

Above is the dropdown picker and the FlatList. In this code, all the array objects and details are displayed in the flat list. But instead of that, I want the select the array item name from the dropdown while using the selected item names' id after I want to display it with the button press function inside the flat list using the above ListItem template. Again while keeping the added array details I want to add another selected array of details to the flat list using the button press button. Is there any way to do it? I tried many methods but every time flat list displays the whole array.

Insted of displaying like this

I want to display like this using the dropdown picker

Shoaib Khan
  • 1,020
  • 1
  • 5
  • 18
Shanuka
  • 23
  • 7
  • You need to write a function which will get called on click of dropdown option and filter the products and change the state setProducts again. (keep the reference of product so that you will not loose the data) – ankitjaiswal Jan 03 '22 at 12:18
  • can you point me to similar documentation or a website – Shanuka Jan 03 '22 at 12:20
  • this is something based on logic... treat it like search feature on the basis of search flatlist gets changed. – ankitjaiswal Jan 03 '22 at 12:23

0 Answers0