[英]React + Typescript: Type of React component with no state/no props
I've seen multiple examples of React components using Typescript:我见过多个使用 Typescript 的 React 组件示例:
class Foo extends React.Component<IProps, IState> {}
It seems there is no a clear convention when we don't use either the Props or the State.当我们不使用 Props 或 State 时,似乎没有明确的约定。
People set these types as any
, null
, undefined
, {}
, , etc.This is what I've seen so far:void
人们将这些类型设置为
any
、 null
、 undefined
、 {}
、 等。这是我目前所看到的:void
class Foo extends React.Component<null, null> {}
class Foo extends React.Component<any, any> {}
class Foo extends React.Component<{}, {}> {}
class Foo extends React.Component<undefined, undefined> {}
class Foo extends React.Component<void, void> {}
class Foo extends React.Component<object, object> {}
What's the best way of doing it?最好的方法是什么?
Update:更新:
Props:道具:
void
cannot be used ( https://github.com/Microsoft/TypeScript/issues/15409 and https://github.com/Microsoft/TypeScript/issues/15419 ) as the props object is initialised to {}
void
( https://github.com/Microsoft/TypeScript/issues/15409和https://github.com/Microsoft/TypeScript/issues/15419 ),因为 props 对象被初始化为{}
SOLUTION解决方案
Simply do - class Foo extends React.Component {}
as prop and state are initialised to {}
简单地做 -
class Foo extends React.Component {}
作为 prop 和 state 被初始化为{}
From https://github.com/DefinitelyTyped/DefinitelyTyped/blob/15b7bac31972fbc081028937dfb1487507ca5fc9/types/react/index.d.ts#L199-L200来自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: props 和 state 被初始化为
{}
,所以对于没有 state 和 prop 的组件,我们可以简单地做:
class Foo extends React.Component {}
According to this guideline and my exp, I would say :根据本指南和我的经验,我会说:
class Foo extends React.Component<null, null> {}
when you know you will not recieve props nor state class Foo extends React.Component<null, null> {}
当你知道你不会收到 props 或 state 时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 class Foo extends React.Component<any, any> {}
当你知道你会收到 props 和 state 但你真的不关心它们的样子class Foo extends React.Component<{}, {}> {}
never saw, seems strange class Foo extends React.Component<{}, {}> {}
见过,看起来很奇怪class Foo extends React.Component<undefined, undefined> {}
same as null
, it's up to you. class Foo extends React.Component<undefined, undefined> {}
与null
相同,这取决于您。 I see more often null
than undefined
null
不是undefined
class Foo extends React.Component<void, void> {}
bad idea, since seems to be reserved for functions return (when you do not expect one) class Foo extends React.Component<void, void> {}
坏主意,因为它似乎是为函数返回保留的(当你不期望的时候) Other opinions are welcomed欢迎其他意见
As answered for this question , you can use the React.FC<{}>
class 对于这个问题的回答,您可以使用
React.FC<{}>
类
const MyStatelessComponent : React.FC<{}> = props =>
<div>{props.children}</div>
Or if your markup gets bigger:或者,如果您的标记变大:
const MyStatelessComponent : React.FC<{}> = props => {
{/* Some code here */}
return <div>{props.children}</div>
}
I always create a props Interface for each component, even if it's blank.我总是为每个组件创建一个 props 接口,即使它是空白的。 It keeps things consistant and allows me to easily add props later if needed.
它使事情保持一致,并允许我稍后在需要时轻松添加道具。
Interface FooProps { }
class foo extends React.Component<FooProps, any> {
}
You can use VoidFunctionComponent
for stateless ans propless components (without state ana props):您可以将
VoidFunctionComponent
用于无状态和无道具组件(没有状态和道具):
const MyComponent: React.VoidFunctionComponent = () => {
...
}
Stateful(class based components) & Stateless components there's a lot of conceptions on the internet about when use one or another, I've grasped these concepts using this list (before have practical experience):有状态(基于类的组件)和无状态组件在互联网上有很多关于何时使用一种或另一种的概念,我已经使用这个列表掌握了这些概念(在有实践经验之前):
Stateless无国籍
Examples: Menu, UserInfo, List, SideBar.示例:菜单、用户信息、列表、边栏。
Stateful有状态的
Examples: UserPage, FollowersSidebar, ArticlesContainer.示例:UserPage、FollowersSidebar、ArticlesContainer。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.