Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6213

How to add items in array in react native?

$
0
0

I want to add item.name,item.id,item.price in array onClick of button.Button is in Flatlist so every time I eant to save these items in array and finally I want all these items.

**enter code here**import React, { Component } from 'react';import {Text,View,FlatList, Modal,TouchableOpacity} from 'react-native';import { Style } from './style';import CustomButton from '../../custom-button/custom-button';import { SafeAreaView } from 'react-native-safe-area-context';import ModalComponent from '../Modal/ModalComponent';import CustomIcon from '../CustomIcons/CustomIcon';interface MenuItemsProps{  menuArray:any[];  category_name:string;}interface MenuItemsState{  flag:number;  visible:boolean;  firstModalVisible2:boolean;  firstModalVisible3:boolean;  itemcount:number;  cartArray:any[];// I want to add items in this array}export default class MenuItemsComponent extends Component<MenuItemsProps,MenuItemsState>{    constructor(props:MenuItemsProps){        super(props);        this.state={          flag:0,          visible:false,          firstModalVisible2:false,          firstModalVisible3:false,          itemcount:0,          cartArray:[]        }    }    componentDidMount(){      {this.props.menuArray.map((data) => {          {if(this.props.category_name==data.category_name)            this.setState({              flag:this.state.flag+1            })}}        )}         }    render(){    return (<SafeAreaView style={Style.paddingStyle}>            {this.state.flag!=0?<View><Text style={Style.textStyle}>{this.props.category_name}</Text> <FlatList              data={this.props.menuArray}              renderItem={({item}) =>(<View>                  {item.category_name==this.props.category_name?<View style={{paddingTop:7}}> <View  style={Style.menu_buttonStyle}>        <View><Text style={Style.nameStyle}>{item.name}</Text><Text style={Style.priceStyle}>{'\u20B9'}{item.price}</Text></View><View><CustomButton //onthis button click                          onPress={()=>                              this.setState({itemcount:this.state.itemcount+1})                          }                           text=" Add " outline={true} />                          {/* {this.state.visible&& <ModalComponent  price={item.price} visible={this.state.visible} itemname={item.name}/> } */}</View></View><Text style={Style.descriptionStyle}>{item.description}</Text></View>:null}</View>              )}              keyExtractor={item => item.id}            /></View>:null} </SafeAreaView>    );  }}

This is my code

I want to add item.name,item.id,item.price in array onClick of button.Button is in Flatlist so every time I eant to save these items in array and finally I want all these items.


Viewing all articles
Browse latest Browse all 6213

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>