Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react native Flatlist renderItem not update

I'm using FlatList and It displays all content properly.

But when I'm updating product quantity, it only increases total amount but nothing changes in renderItem of FlatList.

enter image description here

When I press on plus button, total amount is changed but product quantity is not.

How can I update the product quantity? Why the quantity of the FlatList item isn't changed, when the Total amount is updated?

My code:-

constructor(props) {
    super(props);
    this.state = {
        data:[
            {id:4, productName:'Product 4', shortDescription:'shortDescription 4', qty:1, price:500 },
            {id:5, productName:'Product 5', shortDescription:'shortDescription 5', qty:1, price:1000},
            {id:6, productName:'Product 6', shortDescription:'shortDescription 6', qty:1, price:1000},
        ],
    };
}

_addQty = (index) => {
    var data = this.state.data;
    data[index].qty = data[index].qty + 1;
    this.setState(data:data);
}


_getTotalPrice = () => {
    var total = 0;
    for (var i = 0; i < this.state.data.length; i++) {
        total += this.state.data[i].qty * this.state.data[i].price;
    };
    return total;
}


_renderItem = ({item, index}) => {
    var imageCloseWidth = 20;
    var margin = 5;
    var subViewWidth = width-(margin*3);
    return  <View key={index} style={{ marginBottom: margin, marginHorizontal: margin, marginTop: index==0?margin:0}}>
                <View style={{flexDirection: 'row', flex:1}}>
                    <View style={{justifyContent: 'space-between', width:subViewWidth+imageWidth}}>
                        <View>
                            <TouchableOpacity style={{position: 'absolute', right: 0}}>
                                <View><Image style={{height:imageCloseWidth, width:imageCloseWidth, tintColor: '#888888'}} source={MyConstants.imgClose} /></View>
                            </TouchableOpacity>

                            <Text style={[styles.txtProductName, {width:subViewWidth}]}>{item.productName}</Text>
                            <Text style={styles.txtSortDesc}>{item.shortDescription}</Text>
                        </View>
                        <View style={{flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', marginTop:5}}>
                            <View style={{flexDirection: 'row', alignItems: 'center'}}>
                                <TouchableOpacity>
                                    <View><Image style={{height:20, width:20, tintColor: item.qty>1 ? "#888888":"#BBBBBB"}} source={MyConstants.imgMinus} /></View>
                                </TouchableOpacity>
                                <Text style={{marginHorizontal: 5, fontSize: 18}}>{item.qty}</Text>

                                <TouchableOpacity
                                    onPress={ ()=> {
                                        this._addQty(index);
                                    }}>
                                    <View><Image style={{height:20, width:20, tintColor: '#888888'}} source={MyConstants.imgPlush} /></View>
                                </TouchableOpacity>

                            </View>
                            <Text style={{marginHorizontal: 5, fontSize: 18}}>{item.price}</Text>
                        </View>
                    </View>
                </View>
            </View>
}


render() {
    return (
        <View style={styles.container}>
            <FlatList
                renderItem={this._renderItem}
                keyExtractor={ (item,index) => index.toString() }
                data={this.state.data} />
            <View style={{flexDirection:'row', justifyContent: 'space-between'}}>
                <Text style={{flex:3, fontSize: 18}}>Total amount</Text>
                <Text style={{flex:1, fontSize: 18, textAlign:'right'}}>{this._getTotalPrice()}</Text>
            </View>
        </View>
    )
}
like image 595
Bhaumik Surani Avatar asked Sep 06 '18 07:09

Bhaumik Surani


People also ask

How do you refresh a FlatList in React Native?

To implement pull to refresh FlatList with React Native, we can set the refreshing and onRefresh props. to set the refreshing prop to isFetching , which is true when we're getting data for the FlatList . And we set onRefresh to the onRefresh which sets refreshing to true and then set it back to false after 2 seconds.

How do you Rerender FlatList in React Native functional component?

You can force flatlist to rerender by passing the updated list as an extraData prop, i.e extraData={listData} . However, when using functional components a common mistake is passing the same instance of the list data as the prop.

How do I use extraData in FlatList?

flatlist-simple By passing extraData={selectedId} to FlatList we make sure FlatList itself will re-render when the state changes. Without setting this prop, FlatList would not know it needs to re-render any items because it is a PureComponent and the prop comparison will not show any changes.


1 Answers

You might want to add the extraData prop to the flatlist:

<FlatList
  renderItem={this._renderItem}
  keyExtractor={ (item,index) => index.toString() }
  data={this.state.data}
  extraData={this.state}
/>

FlatList extraData:

A marker property for telling the list to re-render (since it implements PureComponent). If any of your renderItem, Header, Footer, etc. functions depend on anything outside of the data prop, stick it here and treat it immutably.

Find it here

like image 158
Poptocrack Avatar answered Nov 07 '22 14:11

Poptocrack