首页 > 其他分享 >React 限制 Props 和 State 类型

React 限制 Props 和 State 类型

时间:2023-03-21 15:45:06浏览次数:48  
标签:PropsType Component React State Props date

下面是 Component 的接口,P 代表 Props、S 代表 State。

interface Component<P = {}, S = {}, SS = any> extends ComponentLifecycle<P, S, SS> { }

所以,在 tsx 中写两个类型进行约束就可以了:

import { Component, useState } from "react";

type StateType = {
  date: Date;
};

type PropsType = {
  name?: string;
};

export default class Clock extends Component<PropsType, StateType> {
  constructor(props: PropsType) {
    super(props);
    this.state = {
      date: new Date(),
    };
  }

  render() {
    return (
      <div></div>
    );
  }
}

标签:PropsType,Component,React,State,Props,date
From: https://www.cnblogs.com/Himmelbleu/p/17240247.html

相关文章