[英]React Native ProgressBarAndroid setState not working
I'm having trouble figuring out the problem with the following code. 我无法通过以下代码解决问题。 I'm trying to change the prop animating on a ProgressBarAndroid, and make it toggle every second. 我正在尝试更改ProgressBarAndroid上的动画效果,并使其每秒切换一次。 The code works as intended if I set loading to true in my constructor, but not if it's set to false (which is what I want, I don't want it to start animating right away). 如果我在构造函数中将load设置为true,则代码按预期工作,但如果将false设置为false,则代码不起作用(这是我想要的,我不希望它立即开始进行动画处理)。 When it's set to false, the progressbar stays invisible all the time. 设置为false时,进度栏始终保持不可见。 Any ideas? 有任何想法吗?
import React, { Component } from 'react';
import { ProgressBarAndroid } from 'react-native';
export default class App extends Component {
constructor(props) {
super(props);
this.state = {loading: false}; // works if it is set to true here instead
// Toggle the state every second
setInterval(() => {
this.setState({loading: !this.state.loading});
}, 1000);
}
render() {
return (
<ProgressBarAndroid animating={this.state.loading}></ProgressBarAndroid>
);
}
}
This is actually an issue I had with ProgressBarAndroid
a few weeks back. 这实际上是几周前我对ProgressBarAndroid
遇到的一个问题。 Once initiated as false
, I was never able to set it back true. 一旦启动为false
,我就无法将其设置为true。
The quick and dirty solution I had at the time was to just to move the state change outside of the animating
prop. 我当时的快速而肮脏的解决方案是将状态更改移到animating
道具之外。
In your case, changing this: 在您的情况下,请更改此:
<ProgressBarAndroid animating={this.state.loading}></ProgressBarAndroid>
to this: 对此:
<View>
{ this.state.loading ? <ProgressBarAndroid/> : null }
</View>
and make sure to also include View
from react-native
. 并确保还包含View
from react-native
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.