简体   繁体   English

React + Typescript:无状态/无道具的 React 组件类型

[英]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 , {} , void , etc.This is what I've seen so far:人们将这些类型设置为anynullundefined{}void等。这是我目前所看到的:

  1. class Foo extends React.Component<null, null> {}
  2. class Foo extends React.Component<any, any> {}
  3. class Foo extends React.Component<{}, {}> {}
  4. class Foo extends React.Component<undefined, undefined> {}
  5. class Foo extends React.Component<void, void> {}
  6. class Foo extends React.Component<object, object> {}

What's the best way of doing it?最好的方法是什么?

Update:更新:

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 :根据本指南和我的经验,我会说:

  1. 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 时
  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 class Foo extends React.Component<any, any> {}当你知道你会收到 props 和 state 但你真的不关心它们的样子
  3. class Foo extends React.Component<{}, {}> {} never saw, seems strange class Foo extends React.Component<{}, {}> {}见过,看起来很奇怪
  4. 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
  5. 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无国籍

  1. Are concerned with how things look.关心事物的外观。
  2. May contain both presentational and container components** inside, and usually have some DOM markup and styles of their own.内部可能包含展示组件和容器组件**,并且通常有一些自己的 DOM 标记和样式。
  3. Often allow containment via this.props.children.通常允许通过 this.props.children 进行收容。
  4. Have no dependencies on the rest of the app, such as Flux actions or stores.不依赖于应用程序的其余部分,例如 Flux 操作或商店。
  5. Don't specify how the data is loaded or mutated.不要指定数据是如何加载或改变的。
  6. Receive data and callbacks exclusively via props.仅通过 props 接收数据和回调。
  7. Are written as functional components被写成功能组件

Examples: Menu, UserInfo, List, SideBar.示例:菜单、用户信息、列表、边栏。

Stateful有状态的

  1. Are concerned with how things work.关心事情是如何运作的。
  2. 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.内部可能包含展示组件和容器组件**,但通常没有任何自己的 DOM 标记,除了一些包装 div,并且从不具有任何样式。
  3. Provide the data and behavior to presentational or other container components.向表示或其他容器组件提供数据和行为。
  4. Call Redux actions and provide these as callbacks to the presentational components.调用 Redux 操作并将它们作为回调提供给展示组件。

Examples: UserPage, FollowersSidebar, ArticlesContainer.示例:UserPage、FollowersSidebar、ArticlesContainer。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM