Skip to content

Commit 33aea10

Browse files
authored
Merge pull request #21 from tpetrina/sfc-comment
thanks for the explanation! learned something today.
2 parents b88c3d7 + 6fc57d6 commit 33aea10

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatshee
9494

9595
## Stateless Functional Components
9696

97-
*Contributed by: [@jasanst](https://github.com/sw-yx/react-typescript-cheatsheet/pull/9)*
97+
*Contributed by: [@jasanst](https://github.com/sw-yx/react-typescript-cheatsheet/pull/9) and [@tpetrina](https://github.com/sw-yx/react-typescript-cheatsheet/pull/21)*
9898

9999
You can specify the type of props as you destructure them:
100100

@@ -108,10 +108,22 @@ Or you can use the provided generic type for functional components:
108108
const App: React.SFC<{ message: string }> = ({ message }) => <div>{message}</div>;
109109
```
110110

111-
Quite frankly I prefer the former pattern as it's shorter.
111+
<details>
112+
113+
<summary><b>Discussion</b></summary>
114+
115+
The former pattern is shorter, so why would people use `React.SFC` at all? If you need to use `children` property inside the function body, in the former case it has to be added explicitly. `SFC<T>` already includes the correctly typed `children` property which then doesn't have to become part of your type.
116+
117+
```tsx
118+
const Title: React.SFC<{ title: string }> = ({ children, title }) => (
119+
<div title={title}>{children}</div>
120+
);
121+
```
112122

113123
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
114124

125+
</details>
126+
115127
## Stateful Class-based Components
116128

117129
Within Typescript, `React.Component` is a generic type (aka `React.Component<PropType, StateType>`), so you actually want to provide it with prop and (optionally) state types:

0 commit comments

Comments
 (0)