Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why FlatList is not updating dynamically in React Native

I'm very new to react native and I'm trying to update list dynamically.

Below is my code:

import React, { Component } from "react";
import { View, Text, StyleSheet, FlatList } from "react-native";
import { Tile, ListItem, List } from "react-native-elements";


export default class JoinSession extends Component {

    constructor() {
        super();

        this.state = {
            dataToRender: [{ "id": "0", "name": "name0", "des": "des0" }]
        }
    }

    componentDidMount() {   
        var counter = 0;
        const interval = setInterval(() => {
            try {
                var temp = {
                    "id": ++counter + "",
                    "name": "name" + counter,
                    "des": "des" + counter
                }

                let tempArray = this.state.dataToRender;
                tempArray.push(temp);

                this.setState({
                    dataToRender: tempArray
                });

                console.log(this.state.dataToRender);

                if(counter === 10) {
                    clearInterval(interval);
                }
            } catch (e) {
                console.log(e.message);
            }
        }, 2000);
    }

    renderList(item) {
        console.log(item);

        return (
            <ListItem
                roundAvatar
                title={item.name}
                subtitle={item.des}
            />
        );
    }

    render() {
        return (
            <View style={{ flex: 1, alignItems: "stretch", backgroundColor: "skyblue" }}>
                <List>

                    <FlatList
                        data={this.state.dataToRender}
                        renderItem={({ item }) => this.renderList(item)}
                        keyExtractor={item => item.id}
                    />
                </List>
            </View>
        );
    }
}

I am only able to get first element which I've declared in the constructor but data which I'm appending in serInterval is not showing up on the page.

Please help me to resolve it or if there is any other way to do it, please let me know.

Thanks in advance.

like image 836
Shubham Avatar asked May 31 '18 09:05

Shubham


People also ask

How to optimize React Native flatlist performance?

FlatList is the component in React native that is used to render a list of items. It works great for basic lists but FlatList will have some performance issues if not optimized properly causing laggy scroll and slow performance. Let’s see how to optimize react native flatlist performance. 1. Avoid arrow functions inline for renderItem

How to make a flatlist re-render when the state changes?

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

How to track item re-ordering in a flatlist?

You can set the keyExtractor to your FlatList component. This prop is used for caching and as the React key to track item re-ordering. You can also use a key prop in your item component. Move out the renderItem function to the outside of render function, so it won't recreate itself each time render function called.

Why flatlist is lazy loading?

This process of lazy loading helps Flatlist to render fast initially. Also, the single item of the array is a Pure component that will not re-render unnecessary and rerendering occurs only when required. You can also create every single item as a memoized item that follows the concept of tree shaking.


1 Answers

Hi try to have a look on the extraData parameter you can use on a FlatList:

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

<FlatList
 ...
 extraData={this.state}
/>

More info on the FlatList documentation: https://facebook.github.io/react-native/docs/flatlist.html

Also you shouldn't need this <List> from react native element here the list behaviour is totally handle by your FlatList.

like image 144
Alex DG Avatar answered Oct 04 '22 07:10

Alex DG