MarkLoading 在組件上方添加蒙層loading

當(dāng)一個組件在loading時需要阻止點擊的時,使用該組件包裹,并傳遞loading,可以在子組件上方添加一層蒙層,并且在中間添加loading效果。

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

interface MarkLoadingTypeProps {
  loading?: boolean;
  children?: React.ReactNode;
}
class MarkLoading extends React.PureComponent<MarkLoadingTypeProps, any> {
  constructor(props: MarkLoadingTypeProps) {
    super(props);
    this.state = {
      height: 0,
    };
  }
  onLayout = (e: {nativeEvent: any}) => {
    console.log(`e-------------`, e);
    // e.nativeEvent.layout.height
    this.setState({
      height: e.nativeEvent.layout.height,
    });
  };
  render(): React.ReactNode {
    const {loading} = this.props;
    const {height} = this.state;
    return (
      <View style={styles.constainer} onLayout={this.onLayout}>
        {loading && (
          <View
            style={[
              styles.loadingContainer,
              {
                height: height,
              },
            ]}>
            <ActivityIndicator size="large" color="#999999" style={styles.ActivityIndicator}/>
          </View>
        )}
        {this.props.children}
      </View>
    );
  }
}

export default MarkLoading;

const styles = StyleSheet.create({
  constainer: {
    flex: 1,
    position: 'relative',
  },
  loadingContainer: {
    position: 'absolute',
    width: '100%',
    zIndex: 99,
    backgroundColor: 'rgba(0, 0, 0, .05)'
  },
  ActivityIndicator: {
    flex: 1
  }
});

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容