Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to hide and show loading spinner - Activity Indicator react native, managing props and state

I have created a custom Activity Indicator class and I want to control the hide/show of it from where I use it.

Here is what I have done.

CustomActivityIndicator.js

    import React, { Component } from 'react';
import { ActivityIndicator, View, Text, TouchableOpacity, StyleSheet, Dimensions } from 'react-native';
import colors from '../../../styles/colors';
import { consoleLog } from '../../../utils/globalFunctions';

const { width, height } = Dimensions.get('window');

export default class CustomActivityIndicator extends Component {

    constructor(props){
      super(props);
      this.state = {
        show: this.props.show       
      }
    }

    static getDerivedStateFromProps(nextProps, prevState) {

      let outObj = {};
      consoleLog('Login - nextProps.show - ' + nextProps.show + ' prevState.show - ' + prevState.show);    
      if(nextProps.show !== prevState.show) {
        return {
          show: nextProps.show
        };
    }
  }

    render() {
        consoleLog('CustomActivityIndicator - ' + this.state.show  );
      return (
        <View style={styles.container}>
         <ActivityIndicator
               animating = {this.state.show}
               color = {colors.primaryColor}
               size = "large"/>
      </View>
      );
    }
  }

const styles = StyleSheet.create ({
   container: {
      position: 'absolute',
      top: 0,
      left: 0,
      right: 0,
      bottom: 0,
      justifyContent: 'center',
      alignItems: 'center'
   }
})

I am using in Login this is just to demonstrate

I am initially setting the show state to false in Login and When I click the Login button I want to show the ActivityIndicator.

Can you guide me on where I am getting it wrong.

Login.js

 class Login extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      show: false
   };
  }


  loginEndpointDecider = () => {
    this.setState({show: true})  ;
  }

 render() {
    return (            
      <ScrollView style={styles.mainContainer}>    
        <CustomActivityIndicator show={this.state.show}/>         
        <TouchableOpacity title='Transactions'
          style = {{ height: 60, backgroundColor: '#673fb4', marginTop: 20, alignItems: 'center', justifyContent: 'center' }}
          onPress={() => {          
            this.loginEndpointDecider();
          }}>
          <CommonText style={{ color: 'white'}}>
            {strings.signInLower}
          </CommonText>
        </TouchableOpacity>            
      </ScrollView>
    );
  }
}

Thanks R

like image 515
BRDroid Avatar asked Oct 05 '18 15:10

BRDroid


People also ask

How do I show and hide activity indicator in react native?

The animating prop is used to show and hide the activity indicator in react native. The animating prop support two values True(To Show ActivityIndicator ) and False (To Hide ActivityIndicator ).

How do you show and hide spinner in react?

You can hide spinner in the ProgressButton by setting the e-hide-spinner property to cssClass .

How do you show loading indicator in react native?

import { ActivityIndicator} from 'react-native'; Here are some of the important properties available with ActivityIndicator. For animating the loading indicator.It takes boolean value true to show the indicator and false to hide it. Color to be shown for the loading indicator.


1 Answers

Instead of having all the props inside the actual component itself - a better way in the "React mindset" is to be able to conditionally render a flexible component. What this means is that inside your Login.js file you can use the state to display something inside the render method.

class Login extends React.Component {



constructor(props) {
    super(props);
    this.state = {
      show: false
   };
  }


  loginEndpointDecider = () => {
    this.setState({show: true})  ;
  }

 render() {
    return (            
      <ScrollView style={styles.mainContainer}>    
        {this.state.show ? <CustomActivityIndicator/> : "not shown"}       
        <TouchableOpacity title='Transactions'
          style = {{ height: 60, backgroundColor: '#673fb4', marginTop: 20, alignItems: 'center', justifyContent: 'center' }}
          onPress={() => {          
            this.loginEndpointDecider();
          }}>
          <CommonText style={{ color: 'white'}}>
            {strings.signInLower}
          </CommonText>
        </TouchableOpacity>            
      </ScrollView>
    );
  }
}

The {this.state.show ? <CustomActivityIndicator/> : "not shown"} is shorthand for an if statement.

like image 93
Perniferous Avatar answered Sep 23 '22 00:09

Perniferous