简体   繁体   English

如何使用 TypeScript 在 React 组件类上声明 defaultProps?

[英]How to declare defaultProps on a React component class using TypeScript?

Could anyone show an example of defining defaultProps on a React component class in TypeScript?任何人都可以展示在 TypeScript 中的 React 组件类上定义defaultProps的示例吗?

interface IProps {}
interface IState {}

class SomeComponent extends Component<IProps, IState> {
    // ... defaultProps ?
    // public defaultProps: IProps = {}; // This statement produces an error

    constructor(props: IProps) {
        super(props);
    }

    // ...
}

You can define default props this way:您可以通过以下方式定义默认道具:

export class Counter extends React.Component {
  constructor(props) {
    super(props);
    this.state = {count: props.initialCount};
    this.tick = this.tick.bind(this);
  }
  tick() {
    this.setState({count: this.state.count + 1});
  }
  render() {
    return (
      <div onClick={this.tick}>
        Clicks: {this.state.count}
      </div>
    );
  }
}
Counter.propTypes = { initialCount: React.PropTypes.number };
Counter.defaultProps = { initialCount: 0 };

This is equivalent in TypeScript to defining defaultProps as a static field inside the class body:这在 TypeScript 中等同于将 defaultProps 定义为类体内的静态字段:

class SomeComponent extends Component<IProps, IStates> {
    public static defaultProps: IProps = { /* ... */ }; 
    // ...
}

Let say if you have a Movie stateless component then you can define proptype like this below:假设你有一个 Movie 无状态组件,那么你可以像下面这样定义 proptype:

const Movie = props => {
    return (
       <div>
         <h3>{props.movie.title}</h3>
       </div>
    );
 };

Movie.propTypes = {
  movie: PropTypes.shape({
     title: PropTypes.string.isRequired
  })
};

Movie. defaultProps = {
  movie: PropTypes.shape({})
};

And for class component you can do something like this or you can use the same pattern like above:对于类组件,您可以执行以下操作,也可以使用与上面相同的模式:

export default class Movie extends React.Component {
  static propTypes = {
  movie: PropTypes.shape({
  title: PropTypes.string.isRequired
  }),
  desc: PropTypes.string
};

 static defaultProps = {
  desc: 'No movie is available'
 };

render() {
  return (
     <div>
        <h3>{this.props.movie.title}</h3>
        <h3>{this.props.movie.desc}</h3>
     </div>
    );
 }
}

I used if-statements to check if the value of the prop was undefined, if so, I set a default value, otherwise I used the value that was passed through.我使用 if 语句来检查 prop 的值是否未定义,如果是,我设置一个默认值,否则我使用传递过来的值。

interface Props {
  style: any;
  bounces?: boolean | undefined;
  extraHeight?: number | undefined;
}

const DynamicView: React.FC<Props> = (props) => {
  return (
    <KeyboardAwareScrollView
      style={props.style}
      bounces={
        (props.bounces = props.bounces === undefined ? false : props.bounces)
      }
      extraHeight={
        (props.extraHeight =
          props.extraHeight === undefined ? 15 : props.extraHeight)
      }>
      {props.children}
    </KeyboardAwareScrollView>
  );
};

export default DynamicView;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM