Show loader when button is clicked in react native

放肆的年华 提交于 2019-12-03 15:59:57

I tried to simplify your code to its logical skeleton. I hope this will work and you can start adding styling and etc.

export default class LoginButton extends Component {
  state = {
    isLoading: false
  };

  showLoader = () => {
    this.setState({ isLoading: true });
  };

  render() {
    return (
      <View>
        <TouchableOpacity onPress={this.showLoader}>
          <Text>Sign Up Now</Text>
        </TouchableOpacity>
        <ActivityIndicator animating={this.state.isLoading} />
      </View>
    );
  }
}

Issue is with the position of ActivityIndicator.

Try removing styling of the Container View, loader will be visible.

  <View>
     <ActivityIndicator animating={this.state.showLoader} size="small" 
        color="#00ff00" />
   </View>

I've made some corrections to your existing code.

I'm mentioning some key changes here:

  1. View structure
  2. Position in activity indicator view

    constructor(props) {
      super(props);
      this.doSignup = this.doSignup.bind(this);
      this.state = {
        showLoader:false
      }
    }
      showLoader = () => { this.setState({ showLoader:true }); };
      hideLoader = () => { this.setState({ showLoader:false }); };
    
     doSignup(){
       this.showLoader();
     }
    render() {
      return (
         <View style={{flex:1}}>
            <TouchableOpacity style={{ marginTop: 25 }} onPress={this.doSignup}>
              <View style={[ styles.button, { backgroundColor: '#5a0060' } ]}>
               <Text style={{ fontSize: 20, color: "white" }}>Sign Up Now</Text>
            </View>
           </TouchableOpacity>
    
        <View style={{ position: 'absolute', top:"50%",right: 0, left: 0 }}>
          <ActivityIndicator animating={this.state.showLoader} size="large" color="red" />
        </View>
    </View>
      );
     }
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!