我正在尝试在我的 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/