1.0.1 • Published 4 years ago

react-native-fetch-list v1.0.1

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

React Native Fetch List

This component will manage data loading, rereshing and loading more with a fetch prop which returns a promise. You don't need to do these by youself. Rendering with Flatlist.

Usage

props

Other props will be spread onto FlatList

NameTypedescription
fetch(params: { page: number; limit: number }) => Promise<T[]>how to get data
limitnumber, optionaldata limit per page, default 10
dataT[], optionalif you want to manage the data by yourself, use this prop
onChange(data: T[]) => void, optionalbe triggered when data change
filter(item: T) => boolean, optionalmethod to filter some data

example

import React, { useRef, useEffect } from 'react';
import { StyleSheet, View, Text } from 'react-native';
import ListView from 'react-native-fetch-list';

const App = () => {
  const keyExtractor = item => item.id;

  const request = ({ page, limit }) => {
    return fetch(
      `https://cnodejs.org/api/v1/topics?page=${page}&limit=${limit}`,
    )
      .then(res => res.json())
      .then(data => data.data);
  };

  const list = useRef();

  useEffect(() => {
    list.current.fetch();
  }, []);

  const renderItem = ({ item }) => (
    <View style={styles.item}>
      <Text>{item.title}</Text>
    </View>
  );

  return (
    <ListView
      ref={list}
      fetch={request}
      keyExtractor={keyExtractor}
      renderItem={renderItem}
      ListEmptyComponent={<Text>no data</Text>}
    />
  );
};

const styles = StyleSheet.create({
  item: {
    paddingVertical: 10,
    paddingHorizontal: 15,
    borderBottomWidth: StyleSheet.hairlineWidth,
    borderBottomColor: '#999',
  },
});

export default App;