加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

reactjs – React Typescript:没有状态/没有道具的React组件的

发布时间:2020-12-15 20:43:04 所属栏目:百科 来源:网络整理
导读:我已经看过使用Typescript的React组件的多个示例: class Foo扩展了React.Component IProps,IState {} 当我们不使用道具或国家时,似乎没有明确的约定. 人们将这些类型设置为any,null,undefined,{},void等.这是我到目前为止看到的: class Foo扩展了React.Com
我已经看过使用Typescript的React组件的多个示例:

class Foo扩展了React.Component< IProps,IState> {}

当我们不使用道具或国家时,似乎没有明确的约定.

人们将这些类型设置为any,null,undefined,{},void等.这是我到目前为止看到的:

> class Foo扩展了React.Component< null,null> {}
> class Foo扩展了React.Component< any,any> {}
> class Foo扩展了React.Component< {},{}> {}
> class Foo扩展了React.Component< undefined,undefined> {}
> class Foo扩展了React.Component< void,void> {}
> class Foo扩展了React.Component< object,object> {}

这样做的最佳方式是什么?

更新:

>道具:

>无法使用void(https://github.com/Microsoft/TypeScript/issues/15409和https://github.com/Microsoft/TypeScript/issues/15419),因为props对象初始化为{}

简单地说 – 类Foo将React.Component {}扩展为prop,并将状态初始化为{}

从 https://github.com/DefinitelyTyped/DefinitelyTyped/blob/15b7bac31972fbc081028937dfb1487507ca5fc9/types/react/index.d.ts#L199-L200起
interface Component<P = {},S = {}> extends ComponentLifecycle<P,S> { }

道具和状态初始化为{},因此对于没有状态或道具的组件,我们可以简单地执行:

class Foo extends React.Component {}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读