【问题标题】:Component's overlay doesn't disappear even after state changes in react native即使在 react native 中的状态更改后,组件的覆盖也不会消失
【发布时间】:2016-10-12 14:05:29
【问题描述】:

下面是我的代码:

// Styling for the common loader
const loader = StyleSheet.create({
  centering: {
    flex: 1,
    position: 'absolute',
    top: 0,
    left: 0,
    right: 0,
    bottom: 0,
    padding: 8,
    zIndex: 1005,
    backgroundColor: '#fff',
    opacity: 0.8
  },
});

// State
this.state = {
   animating: false
};

// Component
{
  this.state.animating ?
  <ActivityIndicator
    animating={this.state.animating}
    color="#8bcb43"
    style={loader.centering}
    size="large"
  />
    :
  null
}

我附上了当 this.state.animating 为 true 和 false 时加载程序的外观截图。

我很惊讶为什么当this.state.animating 为假时组件没有消失。我不确定我做错了什么。

【问题讨论】:

  • 您找到解决此问题的方法了吗?我现在也在为同样的事情苦苦挣扎..
  • @irruputuncu 我已经写了一个答案来帮助。我不确定这是否是最好的解决方案。

标签: android reactjs react-native react-native-android activity-indicator


【解决方案1】:

九个月前我在使用React Native 0.35.0 时遇到了这个问题,这就是我当时所做的,因为我没有找到任何合适的解决方案:

import React, { Component } from 'react';

import {
  StyleSheet,
  ActivityIndicator
} from 'react-native';

// Styling for the common loader
const loader = StyleSheet.create({
  centering: {
    flex: 1,
    position: 'absolute',
    top: 0,
    left: 0,
    right: 0,
    bottom: 0,
    zIndex: 5,
    backgroundColor: '#fff',
    opacity: 0.8
  },

  hideIndicator: {
    position: 'absolute',
    top: -100,
    opacity: 0
  }
});

export default class Loader extends Component {

  render() {
    /* The reason for adding another activity indicator was to hide the first one */
    /* At the time of development, ActivityIndicator had a bug of not getting hidden */
    return (
      <ActivityIndicator
        animating={ this.props.animating }
        color="#8bcb43"
        style={ this.props.animating ? loader.centering : loader.hideIndicator }
        size="large"
      />
    );
  }
}

我切换加载状态的样式。

style={ this.props.animating ? loader.centering : loader.hideIndicator }

我不确定这个问题是否仍然存在,但如果您面临同样的问题,那么我希望这个答案会有所帮助。

【讨论】:

    猜你喜欢
    • 2021-07-26
    • 2022-11-04
    • 2021-04-09
    • 2021-12-25
    • 2021-09-29
    • 1970-01-01
    • 2021-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多