Skip to content

Commit 3275275

Browse files
authored
Merge pull request #52 from jxom/patch-1
Convert React.SFC to React.FunctionComponent
2 parents 0bf912c + 0362cb8 commit 3275275

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ Translations: [中文翻译](https://github.com/fi3ework/blog/tree/master/react-
1414
* [React + TypeScript Starter Kits](#react--typescript-starter-kits)
1515
* [Import React](#import-react)
1616
- [Section 2: Getting Started](#section-2-getting-started)
17-
* [Stateless Functional Components](#stateless-functional-components)
18-
* [Stateful Class-based Components](#stateful-class-based-components)
17+
* [Function Components](#function-components)
18+
* [Class Components](#class-components)
1919
* [Typing DefaultProps](#typing-defaultprops)
2020
* [Extracting Prop Types](#extracting-prop-types)
2121
* [Types or Interfaces?](#types-or-interfaces)
@@ -113,7 +113,7 @@ Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatshee
113113

114114
# Section 2: Getting Started
115115

116-
## Stateless Functional Components
116+
## Function Components
117117

118118
*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)*
119119

@@ -123,20 +123,20 @@ You can specify the type of props as you destructure them:
123123
const App = ({ message }: { message: string }) => <div>{message}</div>;
124124
```
125125

126-
Or you can use the provided generic type for functional components:
126+
Or you can use the provided generic type for function components:
127127

128128
```tsx
129-
const App: React.SFC<{ message: string }> = ({ message }) => <div>{message}</div>;
129+
const App: React.FunctionComponent<{ message: string }> = ({ message }) => <div>{message}</div>;
130130
```
131131

132132
<details>
133133

134134
<summary><b>Discussion</b></summary>
135135

136-
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.
136+
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.
137137

138138
```tsx
139-
const Title: React.SFC<{ title: string }> = ({ children, title }) => (
139+
const Title: React.FunctionComponent<{ title: string }> = ({ children, title }) => (
140140
<div title={title}>{children}</div>
141141
);
142142
```
@@ -145,7 +145,7 @@ const Title: React.SFC<{ title: string }> = ({ children, title }) => (
145145

146146
</details>
147147

148-
## Stateful Class-based Components
148+
## Class Components
149149

150150
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:
151151

@@ -280,7 +280,7 @@ Instead of defining prop types *inline*, you can declare them separately (useful
280280

281281
```tsx
282282
type AppProps = { message: string }
283-
const App: React.SFC<AppProps> = ({ message }) => <div>{message}</div>;
283+
const App: React.FunctionComponent<AppProps> = ({ message }) => <div>{message}</div>;
284284
```
285285

286286
You can also do this for stateful component types (really, any types):
@@ -950,18 +950,18 @@ interface Button {
950950
as: React.ComponentClass | 'a'
951951
}
952952
953-
const Button: React.SFC<Button> = (props) => {
953+
const Button: React.FunctionComponent<Button> = (props) => {
954954
const {as: Component, children, ...rest} = props
955955
return (
956956
<Component className="button" {...rest}>{children}</Component>
957957
)
958958
}
959959
960-
const AnchorButton: React.SFC<AnchorProps> = (props) => (
960+
const AnchorButton: React.FunctionComponent<AnchorProps> = (props) => (
961961
<Button as="a" {...props} />
962962
)
963963
964-
const LinkButton: React.SFC<RouterLinkProps> = (props) => (
964+
const LinkButton: React.FunctionComponent<RouterLinkProps> = (props) => (
965965
<Button as={NavLink} {...props} />
966966
)
967967

0 commit comments

Comments
 (0)