Skip to content

Commit 0ce51d2

Browse files
authored
Merge pull request #70 from sw-yx/more-function-component-details
More FunctionComponent details
2 parents 1ac2dec + e581170 commit 0ce51d2

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

README.md

+11-5
Original file line numberDiff line numberDiff line change
@@ -121,23 +121,29 @@ const App = ({ message }: { message: string }) => <div>{message}</div>;
121121
Or you can use the provided generic type for function components:
122122

123123
```tsx
124-
// React.FC also works
125-
const App: React.FunctionComponent<{ message: string }> = ({ message }) => <div>{message}</div>;
124+
// React.FunctionComponent also works
125+
const App: React.FC<{ message: string }> = ({ message }) => <div>{message}</div>;
126126
```
127127

128128
<details>
129129

130-
<summary><b>Whats the difference?</b></summary>
130+
<summary><b>What's the difference?</b></summary>
131131

132-
The former pattern is shorter, so why would people use `React.FunctionComponent` at all? 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.
132+
The former pattern is shorter, so why would people use `React.FunctionComponent` at all? 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. Typing your function explicitly will also give you typechecking and autocomplete on its static properties, like `displayName`, `propTypes`, and `defaultProps`.
133133

134134
```tsx
135135
const Title: React.FunctionComponent<{ title: string }> = ({ children, title }) => (
136136
<div title={title}>{children}</div>
137137
);
138138
```
139139

140-
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
140+
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):
141+
142+
```tsx
143+
const App: React.FunctionComponent<{ message: string }> = function App({ message }) {
144+
return <div{message}</div>;
145+
}
146+
```
141147

142148
</details>
143149

0 commit comments

Comments
 (0)