React + Typescript:无状态/无道具的 React 组件类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44738954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
React + Typescript: Type of React component with no state/no props
提问by jobmo
I've seen multiple examples of React components using Typescript:
我见过多个使用 Typescript 的 React 组件示例:
class Foo extends React.Component<IProps, IState> {}
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
,{}
,,etc.This就是我迄今为止看到: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> {}
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
cannotbe used (https://github.com/Microsoft/TypeScript/issues/15409and 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 被初始化为{}
采纳答案by jobmo
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 {}
回答by soywod
According to this guidelineand my exp, I would say :
根据本指南和我的经验,我会说:
class Foo extends React.Component<null, null> {}
when you know you will not recieve props nor stateclass Foo extends React.Component<any, any> {}
when you know you will recieve props and state but you really don't care what they look likeclass Foo extends React.Component<{}, {}> {}
never saw, seems strangeclass Foo extends React.Component<undefined, undefined> {}
same asnull
, it's up to you. I see more oftennull
thanundefined
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<null, null> {}
当您知道您不会收到道具或声明时class Foo extends React.Component<any, any> {}
当你知道你会收到道具和状态但你真的不在乎它们的样子class Foo extends React.Component<{}, {}> {}
没见过,感觉怪怪的class Foo extends React.Component<undefined, undefined> {}
一样null
,这取决于你。我看到的次数null
比undefined
class Foo extends React.Component<void, void> {}
坏主意,因为似乎是为函数返回保留的(当你不期望一个时)
Other opinions are welcomed
欢迎其他意见
回答by Leone
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>
}
回答by SteveKitakis
I always create a props Interface for each component, even if it's blank. It keeps things consistant and allows me to easily add props later if needed.
我总是为每个组件创建一个 props 接口,即使它是空白的。它使事情保持一致,并允许我稍后在需要时轻松添加道具。
Interface FooProps { }
class foo extends React.Component<FooProps, any> {
}
回答by Yuri Ramos
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
无国籍
- Are concerned with how things look.
- May contain both presentational and container components** inside, and usually have some DOM markup and styles of their own.
- Often allow containment via this.props.children.
- Have no dependencies on the rest of the app, such as Flux actions or stores.
- Don't specify how the data is loaded or mutated.
- Receive data and callbacks exclusively via props.
- Are written as functional components
- 关心事物的外观。
- 内部可能包含展示组件和容器组件**,并且通常有一些自己的 DOM 标记和样式。
- 通常允许通过 this.props.children 进行收容。
- 不依赖于应用程序的其余部分,例如 Flux 操作或商店。
- 不要指定数据是如何加载或改变的。
- 仅通过 props 接收数据和回调。
- 写成功能组件
Examples: Menu, UserInfo, List, SideBar.
示例:菜单、用户信息、列表、边栏。
Stateful
有状态的
- Are concerned with how things work.
- May contain both presentational and container components** inside but usually don't have any DOM markup of their own except for some wrapping divs, and never have any styles.
- Provide the data and behavior to presentational or other container components.
- Call Redux actions and provide these as callbacks to the presentational components.
- 关心事情是如何运作的。
- 内部可能同时包含展示组件和容器组件**,但通常没有任何自己的 DOM 标记,除了一些包装 div,并且从不具有任何样式。
- 向表示或其他容器组件提供数据和行为。
- 调用 Redux 操作并将它们作为回调提供给展示组件。
Examples: UserPage, FollowersSidebar, ArticlesContainer.
示例:UserPage、FollowersSidebar、ArticlesContainer。