在react native中单击按钮时显示加载程序

Leo*_*eon 5 javascript loader reactjs react-native

我正在尝试在我的react native应用中实现加载器动画,但是单击动画后它不会触发加载器,尽管动画已经更改为true

在下面查看我的代码。

componentWillMount(){
    this.hideLoader();
}

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

doSignup = () => {
    this.showLoader();
}

render() {
    console.log(this.state.showLoader);
    return (
        <View>
            <TouchableOpacity style={{ marginTop: 25 }} onPress={this.doSignup}>
              <View style={[ styles.button, { backgroundColor: '#5a0060' } ]}>
                <Text style={{ fontSize: 20, color: Colors.white }}>Sign Up Now</Text>
              </View>
            </TouchableOpacity>

            <View style={{ position: 'absolute', top: 0, bottom: 0, right: 0, left: 0 }}>
              <ActivityIndicator animating={this.state.showLoader} size="small" color="#00ff00" />
            </View>
        </View>
    );
}
Run Code Online (Sandbox Code Playgroud)

加载屏幕时,将加载器动画设置为false。单击该按钮时,仅true应将其设置为应能够使加载器动起来的动画,但不会出现。

gaz*_*rgo 7

我试图将您的代码简化为其逻辑框架。我希望这会奏效,您可以开始添加样式等。

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

  doSignup = async () => {
    this.setState({ isLoading: true });
    await asyncSignupFunction();
    this.setState({ isLoading: false })
  };

  render() {
    return (
      <View>
        <TouchableOpacity onPress={this.doSignup}>
          <Text>Sign Up Now</Text>
        </TouchableOpacity>
        <ActivityIndicator animating={this.state.isLoading} />
      </View>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)


小智 4

问题在于 ActivityIndi​​cator 的位置。

尝试删除容器视图的样式,加载程序将可见。

  <View>
     <ActivityIndicator animating={this.state.showLoader} size="small" 
        color="#00ff00" />
   </View>
Run Code Online (Sandbox Code Playgroud)