Skip to content

Commit 0794f1d

Browse files
Retsamferdaber
authored andcommitted
BASIC - Rework Function Components section to prefer normal function … (#104)
1 parent 007d5e7 commit 0794f1d

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

README.md

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -145,30 +145,30 @@ Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatshee
145145

146146
## Function Components
147147

148-
You can specify the type of props as you use them and rely on type inference:
148+
These can be written as normal functions that take a `props` argument and return a JSX element.
149149

150150
```tsx
151-
const App = ({ message }: { message: string }) => <div>{message}</div>;
151+
type AppProps { message: string }; /* could also use interface */
152+
const App = ({ message }: AppProps) => <div>{message}</div>;
152153
```
153154

154-
Or you can use the provided generic type for function components:
155+
<details>
156+
157+
<summary><b>What about `React.FC`/`React.FunctionComponent`?</b></summary>
158+
159+
You can also write components with `React.FunctionComponent` (or the shorthand `React.FC`):
155160

156161
```tsx
157162
const App: React.FC<{ message: string }> = ({ message }) => (
158163
<div>{message}</div>
159-
); // React.FunctionComponent also works
164+
);
160165
```
161166

162-
<details>
163-
164-
<summary><b>What's the difference?</b></summary>
167+
Some differences from the "normal function" version:
165168

166-
The former pattern is shorter, so why would people use `React.FunctionComponent` at all?
169+
- It provides typechecking and autocomplete for static properties like `displayName`, `propTypes`, and `defaultProps` - **However**, there are currently known issues using `defaultProps` with `React.FunctionComponent`. See [this issue for details](https://github.com/sw-yx/react-typescript-cheatsheet/issues/87) - scroll down to our `defaultProps` section for typing recommendations there.
167170

168-
- If you need to use `children` property inside the function body, in the former case it has to be added explicitly. `FunctionComponent<T>` already includes the correctly typed `children` property which then doesn't have to become part of your type.
169-
- Typing your function explicitly will also give you typechecking and autocomplete on its static properties, like `displayName`, `propTypes`, and `defaultProps`.
170-
- _In future_, it will also set `readonly` on your props just like `React.Component<T>` does.
171-
- HOWEVER, there are currently known issues using `defaultProps` with `React.FunctionComponent`. See [this issue for details](https://github.com/sw-yx/react-typescript-cheatsheet/issues/87) - scroll down to our `defaultProps` section for typing recommendations there.
171+
- It provides an implicit definition of `children` (see below) - however there are some issues with the implicit `children` type (e.g. [DefinitelyTyped#33006](https://github.com/DefinitelyTyped/DefinitelyTyped/issues/33006)), and it might considered better style to be explicit about components that consume `children`, anyway.
172172

173173
```tsx
174174
const Title: React.FunctionComponent<{ title: string }> = ({
@@ -177,13 +177,11 @@ const Title: React.FunctionComponent<{ title: string }> = ({
177177
}) => <div title={title}>{children}</div>;
178178
```
179179

180-
If you want to use the `function` keyword instead of an arrow function, you can use this syntax (using a function expression, instead of declaration):
180+
- _In the future_, it may automatically mark props as `readonly`, though that's a moot point if the props object is destructured in the constructor.
181181

182-
```tsx
183-
const App: React.FunctionComponent<{ message: string }> = function App({ message }) {
184-
return <div>{message}</div>;
185-
}
186-
```
182+
- `React.FunctionComponent` is explicit about the return type, while the normal function version is implicit (or else needs additional annotation).
183+
184+
In most cases it makes very little difference which syntax is used, but the `React.FC` syntax is slightly more verbose without providing clear advantage, so precedence was given to the "normal function" syntax.
187185

188186
</details>
189187

0 commit comments

Comments
 (0)