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
{this.state.show && <CustomActivityIndicator />}
does?. I do not understand. will that solve the issue I am facing? – Heel