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

2

5

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

Heel answered 5/10, 2018 at 15:22 Comment(0)
D
5

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.

Demineralize answered 5/10, 2018 at 16:19 Comment(0)
A
3

How about wrapping the ActivityIndicator with curly braces and the state value of show like so:

{this.state.show && <CustomActivityIndicator />}

I don't think you'd really need the show prop in that case.

Atwood answered 5/10, 2018 at 15:33 Comment(3)
can you please explain what this line {this.state.show && <CustomActivityIndicator />} does?. I do not understand. will that solve the issue I am facing?Heel
I feel the CustomActivityIndicator can do without the show prop. If the snippet I provide works, it will mean that the value of show in state conditionally renders the CustomActivityIndicatorAtwood
@BRDroid. The answer is detailed as if this.state.show = true execute <CustomActivityIndicator> else do nothing so if this.state.show is false nothing will render.Fourscore

© 2022 - 2024 — McMap. All rights reserved.