1.0.1 • Published 1 year ago

react-native-load-more-flatlist v1.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

React Native Load More Flatlist

A react-native-load-more-flatlist component is based on Flatlist component for React Native. This component will help you to achieve infinite scroll functionality of flatlist with the bottom loader component.

loadmore

Install

  1. Install this package using npm or yarn

with npm:

npm install --save react-native-load-more-flatlist

with yarn:

yarn add react-native-load-more-flatlist
  1. import { LoadMoreFlatlist } from 'react-native-load-more-flatlist';

Api

Props

All props are passed to underlying FlatList

NameTypeDescription
listDataT[]List of items to be rendered.
activityIndicatorStyleViewStyleTo set style to Activity Indicator(bottom loader).
renderFlatlistItem(params: {item:{},index:number) => JSX.ElementFlatlist render item.
onListEndReached() => voidCallback function once list scrolled to the end.
isLoadingbooleanFlag to show or hide bottom loader indicator (required).
indicatorColorstringTo set activity indicator color.
listFooterComponent() => JSX.ElementTo pass custom list footer loader component.
...propsflatlist default propsYou can pass all other flatlist props.

Example

import React, { useState } from 'react'
import { SafeAreaView } from 'react-native'
import  {
  LoadMoreFlatlist,
} from 'react-native-load-more-flatlist';
import ListItem from './components/listItem/ListItem';
import { data } from './data';
import styles from './style';

const App = () => {
  const [listData, setListData] = useState(data)
  const [isLoading, setIsLoading] = useState(false)

  const onListEndReached = async () => {
    setIsLoading(true)
    await setTimeout(() => {
      setListData([...listData, ...data])
      setIsLoading(false)
    }, 2000);
  }

  const renderFlatlistItem = ({ item, index }) => {
    return <ListItem title={item.name} value={item?.value} />
  }
  return (
    <SafeAreaView style={styles.container}>
      <LoadMoreFlatlist
        data={listData}
        onListEndReached={onListEndReached}
        renderFlatlistItem={renderFlatlistItem}
        isLoading={isLoading}
        style={styles.flatlistStyle}
      />
    </SafeAreaView>
  )
}

export default App

Data file (data.tsx)

export const data = [
    {name: 'BitCoin', value: 50},
    {name: 'Crypto', value: 45},
    {name: 'Zipto', value: 58},
    {name: 'CoinZero', value: 75},
    {name: 'Ruppee', value: 69}];

List Item file (ListItem.tsx)

import React, { memo } from 'react'
import { Text, TouchableOpacity, View } from 'react-native'
import styles from './style'

interface props {
  title?: string
  onPress?: Function
  isRajya?: boolean
  value?: any
  setSelectedValue?: any
}

const ListItem: React.FC<props> = (props) => {
  const { title, value } = props
  return (
    <TouchableOpacity style={styles.containerStyle}>
      <View style={styles.wrapperViewstyle}>
        <Text style={styles.textStyle}>{title}</Text>
        <Text style={styles.valueTextStyle}>{value}</Text>
      </View>
    </TouchableOpacity>
  )
}

export default memo(ListItem);

List Item style file (style.ts)

import { StyleSheet } from 'react-native'

const styles = StyleSheet.create({
  containerStyle: {
    padding: 20,
    margin: 10,
    backgroundColor: 'white',
    borderRadius: 4,
    elevation: 5,
  },
  wrapperViewstyle: {
    flexDirection: 'row',
    justifyContent: 'space-between',
  },
  textStyle: {
    fontWeight: '700',
    color: 'black',
  },
  valueTextStyle: {
    fontWeight: '700',
    color: 'black',
  },
})

export default styles