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 web form function communication full of practical information

1. Introduction Earlier we talked about the front...

What do CN2, GIA, CIA, BGP and IPLC mean?

What is CN2 line? CN2 stands for China Telecom Ne...

An example of how to optimize a project after the Vue project is completed

Table of contents 1. Specify different packaging ...

Summary of Commonly Used MySQL Commands in Linux Operating System

Here are some common MySQL commands for you: -- S...

Detailed explanation of Vue project optimization and packaging

Table of contents Preface 1. Routing lazy loading...

How to implement property hijacking with JavaScript defineProperty

Table of contents Preface Descriptors Detailed ex...

How to build pptpd service in Alibaba Cloud Ubuntu 16.04

1. To build a PPTP VPN, you need to open port 172...

10 tips for designing useful, easy-to-use web applications

Here are 10 tips on how to design better-usable w...

Summary of MySQL 8.0 memory-related parameters

Theoretically, the memory used by MySQL = global ...

Detailed explanation of some settings for Table adaptation and overflow

1. Two properties of table reset: ①border-collaps...

Native JS to implement click number game

Native JS implements the click number game for yo...

Simple analysis of EffectList in React

Table of contents EffectList Collection EffectLis...

Introduction to Computed Properties in Vue

Table of contents 1. What is a calculated propert...

MySQL integrity constraints definition and example tutorial

Table of contents Integrity constraints Definitio...