javascript - 在 React Native 中单击按钮时显示加载程序

标签 javascript reactjs react-native loader

我正在尝试在我的 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>
    );
}

加载屏幕时,我将加载器动画设置为 false。单击按钮时仅设置为 true 应该能够为加载程序设置动画但它不会出现。

最佳答案

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

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>
    );
  }
}

关于javascript - 在 React Native 中单击按钮时显示加载程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51763303/

相关文章:

javascript - 在谷歌地图上使用 MarkerWithLabel 隐藏标记

javascript - 在 HOC 中包装数据获取组件以在加载时显示微调器

android - native react -应用程序被杀死/未启动时未收到通知

使用 Expo React Native 构建后 Android APK 无法正常工作

javascript - 使用 Phantomjs 测试 AngularJS

javascript - 在 JavaScript 中,为什么 typeof Function.prototype 是 "function",而不是像其他原型(prototype)对象那样 "object"?

javascript - 为什么 Babel.js 将生成器转换为非顺序开关盒?

javascript - 为什么我无法在 onChange 事件中解析目标的值?

function - ReactJs 全局辅助函数

react-native - react native 文件系统以递归方式读取目录以获取子文件夹和其中的项目