React + Typescript: Type of React component with no state/no props

后端 未结 2 1780
盖世英雄少女心
盖世英雄少女心 2021-02-12 18:50

I\'ve seen multiple examples of React components using Typescript:

class Foo extends React.Component {}

It seems there is no a

相关标签:
2条回答
  • 2021-02-12 19:09

    According to this guideline and my exp, I would say :

    1. class Foo extends React.Component<null, null> {} when you know you will not recieve props nor state
    2. class Foo extends React.Component<any, any> {} when you know you will recieve props and state but you really don't care what they look like
    3. class Foo extends React.Component<{}, {}> {} never saw, seems strange
    4. class Foo extends React.Component<undefined, undefined> {} same as null, it's up to you. I see more often null than undefined
    5. class Foo extends React.Component<void, void> {} bad idea, since seems to be reserved for functions return (when you do not expect one)

    Other opinions are welcomed

    0 讨论(0)
  • 2021-02-12 19:17

    From https://github.com/DefinitelyTyped/DefinitelyTyped/blob/15b7bac31972fbc081028937dfb1487507ca5fc9/types/react/index.d.ts#L199-L200

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

    Props and state are initialised to {}, so for a component with no state nor prop we can simply do:

    class Foo extends React.Component {} 
    
    0 讨论(0)
提交回复
热议问题