在react的TS項目中定義defaultProps


如何在react的TS項目中定義組件的defaultProps,代碼如下:

import React, { Component } from 'react';
import '../../../style/animation/loading.scss';

interface ILoadingProps {
  scale?: number;
}

interface ILoadingStates {
  loadingSize: any;
}

const _defaultProps = { scale: 1 };

class Loading extends Component<ILoadingProps, ILoadingStates> {
  private static defaultProps = _defaultProps; //主要是用 static 關聯當前的class Loading

  constructor(props: ILoadingProps) {
    super(props);
    this.state = {
      loadingSize: {
        transform: 'scale(' + this.props.scale + ')',
        WebkitTransform: 'scale(' + this.props.scale + ')',
        msTransform: 'scale(' + this.props.scale + ')',
        MozTransform: 'scale(' + this.props.scale + ')',
        OTransform: 'scale(' + this.props.scale + ')'
      }
    };
  }

  public render() {
    return <div className='lds-spinner' style={this.state.loadingSize}>
      <div />
      <div />
      <div />
      <div />
      <div />
      <div />
      <div />
      <div />
      <div />
      <div />
      <div />
      <div />
    </div>;
  }
}

// (Loading as any).defaultProps = { scale: 1 }; 這樣定義也可以但是不推薦

export default Loading;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM