React native ScrollView pull down refresh effect

React native ScrollView pull down refresh effect

This article shares the specific code of the pull-down refresh effect of react native ScrollView for your reference. The specific content is as follows

The refreshControl property of ScrollView is used for pull-down refresh and can only be used for vertical views, that is, horizontal cannot be true.

1. Create a custom CKRefresh.js refresh component

import React,{Component} from 'react';
import {
    View,
    Text,
    StyleSheet,
    ScrollView,
    RefreshControl,
    Dimensions
} from 'react-native';

const screenW=Dimensions.get('window').width;

export default class CKRefresh extends Component{
    constructor(){
        super();
        this.state={
            rowDataArr:Array.from(new Array(30)).map((value,index)=>({
                title:'Initialize data'+index
            })),
            //Whether to display loading
            isRefreshing:false,
            loaded:0
        }
    }

    render(){
        const rowsArr=this.state.rowDataArr.map((row,index)=>(<Row data={row} key={index}/>))
        return(
            <ScrollView
                refreshControl={
                    <RefreshControl
                        refreshing={this.state.isRefreshing}
                        onRefresh={()=>this._onRefresh()}
                        colors={['red','green','blue']}
                        title="Loading..."
                    />
                }
            >
                {rowsArr}
            </ScrollView>
        )
    }

    _onRefresh(){
        //1. Display indicator this.setState({
            isRefreshing:true
        });
        //2. Simulate loading data setTimeout(()=>{
            let newDataArr=Array.from(new Array(5)).map((value,index)=>({
                title:'I am the data pulled down'+(this.state.loaded+index)
            })).concat(this.state.rowDataArr);
            //Update state machine this.setState({
                rowDataArr:newDataArr,
                isRefreshing:false,
                loaded:this.state.loaded+5
            });
        },2000);
    }
}

class Row extends Component {
    static defaultProps = {
        data:{}
    };
    render(){
        return(
            <View style={{
                width:screenW,
                height:40,
                borderBottomWidth:1,
                borderBottomColor:'red',
                justifyContent:'center'
            }}>
                <Text>{this.props.data.title}</Text>
            </View>
        )
    }
}

const styles = StyleSheet.create({

})

2. Reference in App.js

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 *
 * @format
 * @flow strict-local
 */

import React from 'react';
import {
  SafeAreaView,
  StyleSheet,
  ScrollView,
  View,
  Text,
  StatusBar,
} from 'react-native';

import {
  Header,
  LearnMoreLinks,
  Colors,
  DebugInstructions,
  ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';

import CKRefresh from './components/CKRefresh';
const App: () => React$Node = () => {

  return (
    <>
      <StatusBar barStyle="dark-content" />
      <SafeAreaView style={styles.mainViewStyle}>
      <CKRefresh/>
      </SafeAreaView>
    </>
  );
};


const styles = StyleSheet.create({
  mainViewStyle:{
      flex:1,
      backgroundColor:'#fff',
  }
});



export default App;

3. The results are as shown in the figure

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • React Native code example based on FlatList pull-down refresh pull-up load
  • React native ListView adds top pull-down refresh and bottom click refresh examples
  • React Native custom pull-down refresh pull-up loaded list example
  • RefreshContorl pull-down refresh usage in React Native
  • React-native ListView pull down to refresh and pull up to load implementation code
  • React implements native pull-down refresh

<<:  How to use Gitlab-ci to continuously deploy to remote machines (detailed tutorial)

>>:  Solve the problem that the MySQL database crashes unexpectedly, causing the table data file to be damaged and unable to start

Recommend

javascript Blob object to achieve file download

Table of contents illustrate 1. Blob object 2. Fr...

Use image to submit the form instead of using button to submit the form

Copy code The code is as follows: <form method...

Detailed explanation of using echarts map in angular

Table of contents Initialization of echart app-ba...

Vue+spring boot realizes the verification code function

This article example shares the specific code of ...

How to add Vite support to old Vue projects

1. Introduction I have taken over a project of th...

Pure JS method to export table to excel

html <div > <button type="button&qu...

How to use axios request in Vue project

Table of contents 1. Installation 2. There is no ...

3 functions of toString method in js

Table of contents 1. Three functions of toString ...

What we have to say about CSS absolute and relative

Written in the opening: Absolute said: "Rela...

How to use module fs file system in Nodejs

Table of contents Overview File Descriptors Synch...

The marquee tag in HTML achieves seamless scrolling marquee effect

The <marquee> tag is a tag that appears in ...

Docker Compose practice and summary

Docker Compose can realize the orchestration of D...

Implementation of Nginx configuration of local image server

Table of contents 1. Introduction to Nginx 2. Ima...

Detailed explanation of Vue life cycle

Table of contents Why understand the life cycle W...