javascript - 在 native react 中隐藏条件 View

标签 javascript react-native

我正在开发 react native 应用程序,我想在加载屏幕之前显示一些加载程序,

我有不同的加载器组件和不同的组件来加载数据,

在 Loader 组件中,我有一个字段 isVisible (true/false),如下所示

constructor(props) {
        super(props);
        this.state = {
        index: 0,
        types: ['CircleFlip', 'Bounce', 'Wave', 'WanderingCubes', 'Pulse', 'ChasingDots', 'ThreeBounce', 'Circle', '9CubeGrid', 'WordPress', 'FadingCircle', 'FadingCircleAlt', 'Arc', 'ArcAlt'],
        size: 100,
        color: "#ff0000",
        isVisible: true
        }

render() {
    var type = this.state.types[this.state.index];

    return (
      <View style={styles.container}>
        <Spinner style={styles.spinner} isVisible={this.state.isVisible} size={this.state.size} type={'ThreeBounce'} color={this.state.color}/>
      </View>
    );
  }

var styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    // backgroundColor: '#d35400',
  },

  spinner: {
    marginBottom: 50
  },

  btn: {
    marginTop: 20
  },

  text: {
    color: "white"
  }
});

在其他组件中,我在从 api 获取数据后渲染 View 。

constructor(props) {
        super(props);
        this.state = {
          tableHead: ['Form Name', 'Download'],
          tableData: [],
          isVisible:true
        }
      }

 componentDidMount(){
          dataSourceRes =getDocumentList(function(dataSourceRes){
             var tableDataRows=[];
             for(let i = 0; i < dataSourceRes.length; i++){
                var arr=[];
                arr.push(dataSourceRes[i].docName, dataSourceRes[i].docPath);
                tableDataRows.push(arr);
             }
             this.setState({
                tableData : tableDataRows
             });


        }.bind(this));
     };
render() {

    const state = this.state;
    const element = (data, index) => (
      <TouchableOpacity onPress={() => this._alertIndex(data)}>
        <View style={styles.btn}>
          <Text style={styles.btnText}>Download</Text>
        </View>
      </TouchableOpacity>
    );

    return (
      <View style={styles.container}>
        <Loader></Loader>
       {/* <Loader> */}
        <ScrollView>
        <Table borderStyle={{borderColor: 'transparent'}}>
          <Row data={state.tableHead} style={styles.head} textStyle={styles.textHeader}/>
          {
            state.tableData.map((rowData, index) => (
              <TableWrapper key={index} style={styles.row}>
                {
                  rowData.map((cellData, cellIndex) => (
                    <Cell key={cellIndex} data={cellIndex === 1 ? element(cellData, index) : cellData} textStyle={styles.text}/>
                  ))
                }
              </TableWrapper>
            ))
          }
        </Table>

        </ScrollView>

        {/* </Loader> */}

      </View>
    )
  }
}

请让我知道如何解决该问题

最佳答案

你可以这样做

class Foo extends React.Component {
  constructor(props) {
    this.state = { loading: true };
  }

  componentDidMount() {
    // Fetch data then set state
    fetch(something).then(() => this.setState({ loading: false }));
  }

  render() {
    if (this.state.loading) {
      return <Loader/>;
    }

    return <MyComponent/>;
  }
}  

关于javascript - 在 native react 中隐藏条件 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57449395/

相关文章:

react-native - React native 实时热重载不适用于 Windows

react-native - React Native-当位置在Android上为绝对位置时, View 消失

javascript - 可能未处理的 promise 拒绝(id :0) Warning

c# - 为什么我不能删除这个 cookie?

react-native - FCM 定期通知在第二天不起作用

javascript - 在 JavaScript 中是否可以添加到回调函数而不是覆盖它?

javascript - NodeJs 加密 key 创建(google kms key 导入) NodeJs

javascript - 如何编写类似于 Redux 的状态更改例程?

javascript - 将 Grunt 集成到 Spring/Gradle 构建中的正确方法是什么?

javascript - 将原型(prototype)附加到 JavaScript 对象