📌  相关文章
📜  react native flatlist pull to refresh - Javascript (1)

📅  最后修改于: 2023-12-03 15:04:48.895000             🧑  作者: Mango

React Native FlatList Pull to Refresh

In this article, we will discuss how to add pull to refresh functionality to a FlatList component in a React Native application. Pull to refresh allows users to update the data in a list by pulling down on the screen.

Getting Started

Before we begin, make sure you have a basic understanding of React Native and FlatList component. We will be using the following libraries:

  • react-native
  • react-native-gesture-handler

You can install these libraries using the following commands:

npm install react-native react-native-gesture-handler --save
Implementation

To add pull to refresh functionality to a FlatList component, we need to use the RefreshControl component provided by React Native.

import React, { useState } from 'react';
import { FlatList, RefreshControl, Text, View } from 'react-native';

const DATA = [
  { id: '1', title: 'Item 1' },
  { id: '2', title: 'Item 2' },
  { id: '3', title: 'Item 3' },
  { id: '4', title: 'Item 4' },
  { id: '5', title: 'Item 5' },
  { id: '6', title: 'Item 6' },
  { id: '7', title: 'Item 7' },
  { id: '8', title: 'Item 8' },
  { id: '9', title: 'Item 9' },
  { id: '10', title: 'Item 10' },
];

const App = () => {
  const [refreshing, setRefreshing] = useState(false);
  const [data, setData] = useState(DATA);

  const onRefresh = () => {
    setRefreshing(true);

    // Fetch new data here and update the state
    const newData = [...data];
    newData.unshift({ id: '0', title: 'New Item' });

    setData(newData);
    setRefreshing(false);
  };

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

  return (
    <FlatList
      data={data}
      renderItem={renderItem}
      keyExtractor={(item) => item.id}
      refreshControl={
        <RefreshControl refreshing={refreshing} onRefresh={onRefresh} />
      }
    />
  );
};

export default App;

In the above code, we have a FlatList component that is displaying a list of items. We have also added a RefreshControl component as a prop to the FlatList. The RefreshControl component takes two props:

  • refreshing: A boolean value that indicates whether the list is refreshing or not.
  • onRefresh: A function that is called when the user pulls down on the screen to refresh the list.

We have initialized the refreshing variable to false and data variable to our initial array of items. When the user pulls down on the screen, the onRefresh function is called. Inside the onRefresh function, we are setting the refreshing variable to true, fetching new data, updating the state, and finally setting the refreshing variable to false.

Lastly, we have a renderItem function that takes an item and returns a view containing the item's title.

Conclusion

In this article, we have discussed how to add pull to refresh functionality to a FlatList component in a React Native application. We have used the RefreshControl component provided by React Native to achieve this functionality.