Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flatlist scroll is not working smoothly and shows an empty white space while scrolling a lagelist in react-native app

I am using the react-native-flatlist grid to scroll the largest data from the server and fetching and displayed in my screen. But the scrolling is not smooth in my app.

Any solution for this?

I am also facing a white space while scrolling the screen. List get disappears and shows a blank white space in UI.

Any solution?

import React, { Component } from 'react';
import {
  ActivityIndicator,
  Image,
  Platform,
  StyleSheet,
  Text,
  FlatList,
  Button,
  View,
  SafeAreaView,
  ScrollView,
  TouchableOpacity,
  PixelRatio,
  Dimensions,
  StatusBar
} from 'react-native';
import _ from 'lodash';
import ImageCropPicker from 'react-native-image-crop-picker';
import Orientation from 'react-native-orientation';
import FastImage from 'react-native-fast-image';

class MyShowroom extends Component {
  constructor() {
    super();
    this.state = {
      index: 0,
      section: 0,
      width: 0,
      height: 0,
      isPageLoaded: false,
      loadingMoreItems: false,
      lastItemIndex: 0,
      page: 0,
    }
    Orientation.addOrientationListener(this._updateOrientation.bind(this))
  }


        renderList(item) {
            return(
              <TouchableOpacity>
                <View style={{backgroundColor: 'white', alignItems: 'center'}}>
                  <FastImage style={{width: gridWidth, height: gridHeight}}
                    resizeMode={FastImage.resizeMode.contain}
                    source={item.uri}/>
                </View>
              </TouchableOpacity>
            );
          }

    // extra data is rendering from the  redux 
        render(){
          return(
            <FlatList
                          ref={(ref) => { this.flatListRef = ref; }}
                          data={Items}
                          renderItem={({ item }) => this.renderList(item)}
                          numColumns={2}
                          extraData={this.props.pagination}
                          keyExtractor={item => item.id}
                          onEndReached={this._handleMoreItems}
                          onEndReachedThreshold={0.001}
                          ListFooterComponent={this._renderFooter}
                          legacyImplementation = {true}
                          bounces = {false}
                          onMomentumScrollEnd={e => this.scroll(e.nativeEvent.contentOffset)}
                        />
          )
        }
like image 696
sejn Avatar asked Oct 29 '19 08:10

sejn


1 Answers

Its as a result of performance issues with the FlatList component but you can add the following props to your FlatList Component, it would help solve the problem. They are: i. removeClippedSubviews. Set this to true, it comes with a default of false. ii. windowSize. Set this to a number like say 30 iii. maxToRenderPerBatch. This controls the number of items rendered per batch, which is the next chunk of items rendered on every scroll.

<FlatList
            data={this.state.contacts}
            removeClippedSubviews={true}
            maxToRenderPerBatch={60}
            windowSize={30}
            ListHeaderComponent={headerComponent}
            contentContainerStyle={{ paddingBottom: 10 }}
            renderItem={({ item }) => (
              <View>
                {item}
              </View>
            )}
            numColumns={1}
            keyExtractor={(item, index) => String(index)}
          />
like image 180
Ichoku Chinonso Avatar answered Oct 27 '22 13:10

Ichoku Chinonso