37

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

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.

People set these types as any, null,undefined,{}, void, etc.This is what I've seen so far:

  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 {}

7 Answers 7

24

As answered for this question, you can use the React.FC<{}> class

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>

}
Sign up to request clarification or add additional context in comments.

2 Comments

React.SFC<{}> can as well be refactored to React.FC<{}> or React.FunctionComponent<{}> since hooks introduced State in functional components.
If you are using TS and it is complaining about FC<{}>, you can reduce it to just FC without the angle and curly brackets. Working example: const MyComponent: FC = () => { /* code and tsx here */ }
18

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 {} 

Comments

13

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

3 Comments

I´ve seen some people having problems with void as a change in a recent version initialises the props to {}
If I used null then I get this error: Type '{}' is not assignable to type 'null'
@Al.G. same here. Obviously missing out the angle brackets is the best if you have neither props nor state,but if you have one or the other, I think {} is the best option
8

You can use VoidFunctionComponent for stateless ans propless components (without state ana props):

const MyComponent: React.VoidFunctionComponent = () => {
  ...
}

1 Comment

React.VoidFunctionComponent has been deprecated in React 18
2

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.

Interface FooProps { }

class foo extends React.Component<FooProps, any> {
}

2 Comments

Do you do the same thing for the state?
You can. I've typically just been leaving that as "any". I'm using react-redux to manage state and the only time I've needed a local state was for input fields.
2

Functional components with no props

Use React.FC without any generic parameter like this:

const ProplessComponent: React.FC = () => {
  // Component implementation
};

React.FC is essentially the same as React.FC<Record<string, never>>, but shorter and more readable.

Don't use React.FC<{}> because {} in Typescript actually means "any non-nullish value".

Functional components with no STATE and no props

With the introduction of React hooks, functional components can manage internal state. Thus, the distinction between stateful and stateless components has become less significant. It's recommended to use React.FC for both types of components.

It's worth noting that React.SFC and React.StatelessComponent are deprecated. It's preferable to use React.FC instead even for stateless components.

Comments

1

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.
  3. Often allow containment via this.props.children.
  4. Have no dependencies on the rest of the app, such as Flux actions or stores.
  5. Don’t specify how the data is loaded or mutated.
  6. Receive data and callbacks exclusively via 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.
  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.

Examples: UserPage, FollowersSidebar, ArticlesContainer.

1 Comment

Thanks for your answer but I meant what of the examples I put is the right way of doing it. I´ve edited the question to make it clearer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.