React native 活动指示器的可见性为';在更改与动画道具绑定的变量值后,t发生了更改

React native 活动指示器的可见性为';在更改与动画道具绑定的变量值后,t发生了更改,react-native,activity-indicator,React Native,Activity Indicator,我需要在从服务器获取数据时显示加载指示器。为此,我补充说 <View> <ActivityIndicator animating={this.state.isLoading} size="large" color="#0000ff" /> </View> 所以,我想在这里显示加载指示器(当我更改变量时,它会影响加载指示器的可见性),但不幸的是什么也没发生。那么,原因是什么?如何解决此问题?视图未指定样式 this.stat

我需要在从服务器获取数据时显示加载指示器。为此,我补充说

<View>
          <ActivityIndicator animating={this.state.isLoading} size="large" color="#0000ff" />
        </View>

所以,我想在这里显示加载指示器(当我更改变量时,它会影响加载指示器的可见性),但不幸的是什么也没发生。那么,原因是什么?如何解决此问题?

视图未指定样式

this.state={
孤岛加载:正确
}
componentDidMount(){
const{getCities}=this.props;
设置超时(()=>{
getCities();
this.setState({isLoading:false})
}, 5000)
}
....
render(){
返回this.state.isLoading===true(
) : (
...

视图
未指定样式

this.state={
孤岛加载:正确
}
componentDidMount(){
const{getCities}=this.props;
设置超时(()=>{
getCities();
this.setState({isLoading:false})
}, 5000)
}
....
render(){
返回this.state.isLoading===true(
) : (
...
有一件事让我很兴奋: -同步设置状态不是一种好的做法,应该使用setState函数

我将重构如下:

displayLoading=()=>{
this.setState({isLoading:true})
}
closeLoading=()=>{
this.setState({isLoading:false})
}
fetchCities=async()=>{
const{getCities}=this.props;
显示加载()
等待
关闭加载()
}
componentDidMount(){
这个
}

NB:对于这个解决方案,我认为您不使用/需要ReDux:)否则,我建议ReDux和一个中间件来自动显示加载到所有API取的

< P> >有一件事使我大吃一惊: -同步设置状态不是一种好的做法,应该使用setState函数

我将重构如下:

displayLoading=()=>{
this.setState({isLoading:true})
}
closeLoading=()=>{
this.setState({isLoading:false})
}
fetchCities=async()=>{
const{getCities}=this.props;
显示加载()
等待
关闭加载()
}
componentDidMount(){
这个
}

NB:对于这个解决方案,我认为你不使用/需要ReDux:否则,我建议redux和一个中间件来自动将显示加载到所有api获取

,不幸的是,这也没有帮助false@SergeiMikhailovskii我更新了我的答案。这样你肯定会在五秒钟内看到指示器。@Sergeimkhailovskii如果你的问题解决了,你能选择我的答案吗?不幸的是,它也没有帮助。在渲染中设置动画只是false@SergeiMikhailovskii我更新了我的答案。这样你肯定会在五秒钟内看到指示器。@Sergeimkhailovskii如果你的问题解决了,你能选择我的答案吗?
componentDidMount() {
    this.state.isLoading = true;
    const { getCities } = this.props;
    getCities();
    this.state.isLoading = false;
  }