Skip to content

Commit c186dca

Browse files
committed
updated react types cheatsheet
1 parent 7a11290 commit c186dca

File tree

2 files changed

+32
-26
lines changed

2 files changed

+32
-26
lines changed

Diff for: README.md

+16-13
Original file line numberDiff line numberDiff line change
@@ -84,54 +84,57 @@ npm i -D @types/react @types/react-dom @types/react-redux
8484

8585
# React Types Cheatsheet
8686

87-
#### `React.StatelessComponent<P>` or alias `React.SFC<P>`
88-
Stateless functional components
87+
#### `React.StatelessComponent<P>` or `React.SFC<P>`
88+
Type representing stateless functional component
8989
```tsx
9090
const MyComponent: React.SFC<MyComponentProps> = ...
9191
```
9292
[⇧ back to top](#table-of-contents)
9393

9494
#### `React.Component<P, S>`
95-
Statefull class component
95+
Type representing statefull class component
9696
```tsx
9797
class MyComponent extends React.Component<MyComponentProps, State> { ...
9898
```
9999
[⇧ back to top](#table-of-contents)
100100
101101
#### `React.ComponentType<P>`
102-
Accepts sfc or class components with Generic Props Type
102+
Type representing union type of (SFC | Component)
103103
```tsx
104104
const withState = <P extends WrappedComponentProps>(
105105
WrappedComponent: React.ComponentType<P>,
106106
) => { ...
107107
```
108108
[⇧ back to top](#table-of-contents)
109109
110-
#### `React.ReactNode`
111-
Accepts any react elements (component instances) and also primitive types
110+
#### `React.ReactElement<P>` or `JSX.Element`
111+
Type representing a concept of React Element - representation of a native DOM component (<div />), or a user-defined composite component (<MyComponent />)
112112
```tsx
113-
const elementOrPrimitive: React.ReactNode = '' || 0 || false || null || <div /> || <MyComponent />;
113+
const elementOnly: React.ReactElement = <div /> || <MyComponent />;
114114
```
115115
[⇧ back to top](#table-of-contents)
116116
117-
#### `JSX.Element`
118-
Similar in usage to ReactNode but limited to accept only react elements (and not primitive types)
117+
#### `React.ReactNode`
118+
Type representing any possible type of React node (basically ReactElement (including Fragments and Portals) + primitive JS types)
119119
```tsx
120-
const elementOnly: JSX.Element = <div /> || <MyComponent />;
120+
const elementOrPrimitive: React.ReactNode = 'string' || 0 || false || null || undefined || <div /> || <MyComponent />;
121+
const Component = ({ children: React.ReactNode }) => ...
121122
```
122-
[⇧ back to top](#table-of-contents)
123+
[⇧ back to top](#table-of-contents
123124
124125
#### `React.CSSProperties`
125-
Type-safety for styles using css-in-js
126+
Type representing style object in JSX (usefull for css-in-js styles)
126127
```tsx
127128
const styles: React.CSSProperties = { flexDirection: 'row', ...
129+
const element = <div style={styles} ...
128130
```
129131
[⇧ back to top](#table-of-contents)
130132

131133
#### `React.ReactEventHandler<E>`
132-
Type-safe event handlers for JSX
134+
Type representing React event handler
133135
```tsx
134136
const handleChange: React.ReactEventHandler<HTMLInputElement> = (ev) => { ...
137+
const element = <input onChange={handleChange} ...
135138
```
136139
[⇧ back to top](#table-of-contents)
137140

Diff for: docs/markdown/1_react.md

+16-13
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,56 @@
11
# React Types Cheatsheet
22

3-
#### `React.StatelessComponent<P>` or alias `React.SFC<P>`
4-
Stateless functional components
3+
#### `React.StatelessComponent<P>` or `React.SFC<P>`
4+
Type representing stateless functional component
55
```tsx
66
const MyComponent: React.SFC<MyComponentProps> = ...
77
```
88
[⇧ back to top](#table-of-contents)
99

1010
#### `React.Component<P, S>`
11-
Statefull class component
11+
Type representing statefull class component
1212
```tsx
1313
class MyComponent extends React.Component<MyComponentProps, State> { ...
1414
```
1515
[⇧ back to top](#table-of-contents)
1616
1717
#### `React.ComponentType<P>`
18-
Accepts sfc or class components with Generic Props Type
18+
Type representing union type of (SFC | Component)
1919
```tsx
2020
const withState = <P extends WrappedComponentProps>(
2121
WrappedComponent: React.ComponentType<P>,
2222
) => { ...
2323
```
2424
[⇧ back to top](#table-of-contents)
2525
26-
#### `React.ReactNode`
27-
Accepts any react elements (component instances) and also primitive types
26+
#### `React.ReactElement<P>` or `JSX.Element`
27+
Type representing a concept of React Element - representation of a native DOM component (<div />), or a user-defined composite component (<MyComponent />)
2828
```tsx
29-
const elementOrPrimitive: React.ReactNode = '' || 0 || false || null || <div /> || <MyComponent />;
29+
const elementOnly: React.ReactElement = <div /> || <MyComponent />;
3030
```
3131
[⇧ back to top](#table-of-contents)
3232
33-
#### `JSX.Element`
34-
Similar in usage to ReactNode but limited to accept only react elements (and not primitive types)
33+
#### `React.ReactNode`
34+
Type representing any possible type of React node (basically ReactElement (including Fragments and Portals) + primitive JS types)
3535
```tsx
36-
const elementOnly: JSX.Element = <div /> || <MyComponent />;
36+
const elementOrPrimitive: React.ReactNode = 'string' || 0 || false || null || undefined || <div /> || <MyComponent />;
37+
const Component = ({ children: React.ReactNode }) => ...
3738
```
38-
[⇧ back to top](#table-of-contents)
39+
[⇧ back to top](#table-of-contents
3940
4041
#### `React.CSSProperties`
41-
Type-safety for styles using css-in-js
42+
Type representing style object in JSX (usefull for css-in-js styles)
4243
```tsx
4344
const styles: React.CSSProperties = { flexDirection: 'row', ...
45+
const element = <div style={styles} ...
4446
```
4547
[⇧ back to top](#table-of-contents)
4648

4749
#### `React.ReactEventHandler<E>`
48-
Type-safe event handlers for JSX
50+
Type representing React event handler
4951
```tsx
5052
const handleChange: React.ReactEventHandler<HTMLInputElement> = (ev) => { ...
53+
const element = <input onChange={handleChange} ...
5154
```
5255
[⇧ back to top](#table-of-contents)
5356

0 commit comments

Comments
 (0)