How do I overlay ActivityIndicator in react-native?

前端 未结 8 2305
没有蜡笔的小新
没有蜡笔的小新 2020-12-13 05:55

I have a View with few form elements and a button (TouchableHighlight). On clicking the button, an Activity Indicator should be shown as an overlay to the existing view. The

相关标签:
8条回答
  • 2020-12-13 06:06

    Add in view of loading

        position: 'absolute',
        left: 0,
        right: 0,
        top: 0,
        bottom: 0,
        alignItems: 'center',
        justifyContent: 'center'
    
    0 讨论(0)
  • 2020-12-13 06:11

    There is a library available for this react-native-loading-spinner-overlay. You can simply install it using

    npm install react-native-loading-spinner-overlay --save
    

    and can import into your project using

    import Spinner from 'react-native-loading-spinner-overlay';
    

    Here is how to use it

    <Spinner
      //visibility of Overlay Loading Spinner
      visible={this.state.loading}
      //Text with the Spinner 
      textContent={'Loading...'}
      //Text style of the Spinner Text
      textStyle={styles.spinnerTextStyle}
    />
    

    0 讨论(0)
  • 2020-12-13 06:13

    I suppose you should use Modal to overlay activity indicator. Following is an example:

    <Modal
    transparent={true}
    animationType={'none'}
    visible={loading}
    onRequestClose={() => {console.log('close modal')}}>
    <View style={styles.modalBackground}>
    <View style={styles.activityIndicatorWrapper}>
    <ActivityIndicator
    animating={loading} />
    </View>
    </View>
    </Modal>
    
    0 讨论(0)
  • 2020-12-13 06:16

    Here is a complete example using create react native app.

    import React from 'react';
    import {StyleSheet, ActivityIndicator, View} from "react-native";
    
    export default class Example extends React.Component {
    
        constructor(props) {
            super(props);
    
            this.state = {}
    
        render() {
            return (
                <View
                    style={{flex: 1}}
                >
                      //Add other content here
                      {this.state.loading &&
                      <View style={styles.loading}>
                          <ActivityIndicator/>
                      </View>
                      }
                    </View>
                );
            }
        }
    
        showLoading() {
           this.setState({loading: true})
        }
    
        hideLoading() {
           this.setState({loading: false})
        }
    
        const styles = StyleSheet.create({
            loading: {
                position: 'absolute',
                left: 0,
                right: 0,
                top: 0,
                bottom: 0,
                opacity: 0.5,
                backgroundColor: 'black',
                justifyContent: 'center',
                alignItems: 'center'
            }
        })
    
    0 讨论(0)
  • 2020-12-13 06:19

    set in View of Activity Indicator

    position: 'absolute', left: 0, right: 0, top: 0, bottom: 0, alignItems: 'center', justifyContent: 'center'

    0 讨论(0)
  • 2020-12-13 06:29

    You can use StyleSheet.absoluteFill to shorten code. Add this to your render:

    <View style={styles.container}>
      //... other code here
      {this.state.loading && <View
          style={{
            ...StyleSheet.absoluteFill,
            justifyContent: 'center',
            alignItems: 'center',
          }}>
          <ActivityIndicator />
      </View>}
    </View>

    Improvement:

    You can also create a Loading component:

    Loading.js

    import React from 'react';
    import {View, ActivityIndicator, StyleSheet} from 'react-native';
    
    export const Loading = ({theme = 'white', size = 'large'}) => {
      const color = theme === 'white' ? '#00bdcd' : '#fff';
      return (
        <View
          style={{
            ...StyleSheet.absoluteFill,
            justifyContent: 'center',
            alignItems: 'center',
          }}>
          <ActivityIndicator size={size} color={color} />
        </View>
      );
    };

    Then use it anywhere you want

    <View style={styles.container}>
       //... other code here
       // remember to import Loading component
       {this.state.loading && <Loading />}
    </View>

    0 讨论(0)
提交回复
热议问题