简体   繁体   English

React Native,如何用FlatList实现pull to refresh?

[英]React Native, how to implement pull to refresh with FlatList?

Here is my attempt:这是我的尝试:

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

const List = () => {
  const data = ['test1', 'test2', 'test3'];
  const [test, setTest] = useState(data);
  return (
    <View style={{alignItems: 'center'}}>
      <Text>List</Text>
      <FlatList
        data={data}
        numColumns={1}
        renderItem={({item, index}) => <Text>{item}</Text>}
        // onRefresh={}

        refreshing={false}
      />
      <Button title="addData" onPress={() => data.push('tset4')} />
      <Button title="show" onPress={() => console.log(data)} />
      <Button title="test" onPress={() => console.log(data)} />
    </View>
  );
};

export default List;

Here how you would implement that (I added comments in the code):在这里你将如何实现它(我在代码中添加了注释):

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

const List = () => {
  const data = ['test1', 'test2', 'test3'];
  const [test, setTest] = useState(data);
  // set up this boolean state
  const [refreshing, setRefreshing] = useState(false);

  // set up this useEffect
  useEffect(()=>{
    if(refreshing){
     // do your heavy or asynchronous data fetching & update your state
     setTest([...test, "test4"]);
     // set the refreshing back to false
     setRefreshing(false);
    }
  },[refreshing])

  return (
    <View style={{alignItems: 'center'}}>
      <Text>List</Text>
      <FlatList
        data={data}
        numColumns={1}
        renderItem={({item, index}) => <Text>{item}</Text>}
        refreshing={refreshing}
        onRefresh={()=>setRefreshing(true)}
      />
      <Button title="addData" onPress={() => data.push('tset4')} />
      <Button title="show" onPress={() => console.log(data)} />
      <Button title="test" onPress={() => console.log(data)} />
    </View>
  );
};

export default List;

You are missing the basics of React.你缺少 React 的基础知识。 Check this example.检查这个例子。

https://snack.expo.dev/@nazrdogan/233ac3 https://snack.expo.dev/@nazrdogan/233ac3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实现拉取刷新 FlatList - Implement pull to refresh FlatList 在React Native的FlatList中,如何在顶部(同时)在底部使用“拉动刷新”? - How to use “Pull to refresh” in top and (at the same time) in bottom in a FlatList in React Native? 如何在FLatList中实现Sraech条 - How to implement Sraech bar in FLatList react native 如何在React Native中使用Flatlist实现多重选择? - How to implement multiselect using Flatlist in react native? 如何实现水平滚动flatlist react native - how to implement horizontal scroll flatlist react native 如何使用 redux react native 添加 pull 以刷新 - how to add pull to refresh with redux react native 如何在提供的组件(尤其是带有fetchPopularMovies的部分)上实现这种依赖关系(react-native-pull-to-refresh)? - How to implement this dependency ( react-native-pull-to-refresh) on the provided component, particularly the part with fetchPopularMovies? 在React Native的当前屏幕上显示时,如何主动刷新FlatList? - How to actively refresh a FlatList whenever it shows on the current screen in React Native? React Native - 如何在返回上一页时刷新FlatList? - React Native - How to refresh FlatList when back to previous page? 从底部功能反应本机平面列表拉取 - React native flatlist pull from bottom functionality
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM