Skip to content

Commit aa3434f

Browse files
committed
Updated React CheatSheet- Added new react type example HTMLProps- Added note about ComponentProps shortcomingsRelated #170
1 parent f402ba6 commit aa3434f

File tree

2 files changed

+48
-32
lines changed

2 files changed

+48
-32
lines changed

Diff for: README.md

+24-16
Original file line numberDiff line numberDiff line change
@@ -123,33 +123,33 @@ npm i -D @types/react @types/react-dom @types/react-redux
123123

124124
# React - Type-Definitions Cheatsheet
125125

126-
#### `React.FunctionComponent<P>` or `React.FC<P>`
126+
#### `React.FC<Props>` | `React.FunctionComponent<Props>`
127127
Type representing a functional component
128128
```tsx
129129
const MyComponent: React.FC<Props> = ...
130130
```
131131

132-
#### `React.Component<P, S>`
132+
#### `React.Component<Props, State>`
133133
Type representing a class component
134134
```tsx
135135
class MyComponent extends React.Component<Props, State> { ...
136136
```
137137
138-
#### `React.ComponentProps<typeof Component>`
139-
Gets type of Component Props, so you don't need to export Props from your component ever! (Works for both FC and Class components)
140-
```tsx
141-
type MyComponentProps = React.ComponentProps<typeof MyComponent>;
142-
```
143-
144-
#### `React.ComponentType<P>`
145-
Type representing union type of (React.FC | React.Component)
138+
#### `React.ComponentType<Props>`
139+
Type representing union of (React.FC<Props> | React.Component<Props>) - used in HOC
146140
```tsx
147141
const withState = <P extends WrappedComponentProps>(
148142
WrappedComponent: React.ComponentType<P>,
149143
) => { ...
150144
```
151145
152-
#### `React.ReactElement` or `JSX.Element`
146+
#### `React.ComponentProps<typeof XXX>`
147+
Gets Props type of a specified component XXX (WARNING: does not work with statically declared default props and generic props)
148+
```tsx
149+
type MyComponentProps = React.ComponentProps<typeof MyComponent>;
150+
```
151+
152+
#### `React.ReactElement` | `JSX.Element`
153153
Type representing a concept of React Element - representation of a native DOM component (e.g. `<div />`), or a user-defined composite component (e.g. `<MyComponent />`)
154154
```tsx
155155
const elementOnly: React.ReactElement = <div /> || <MyComponent />;
@@ -163,22 +163,30 @@ const Component = ({ children: React.ReactNode }) => ...
163163
```
164164
165165
#### `React.CSSProperties`
166-
Type representing style object in JSX (useful for css-in-js styles)
166+
Type representing style object in JSX - for css-in-js styles
167167
```tsx
168168
const styles: React.CSSProperties = { flexDirection: 'row', ...
169169
const element = <div style={styles} ...
170170
```
171171

172-
#### `React.ReactEventHandler<E>`
173-
Type representing generic event handler
172+
#### `React.HTMLProps<HTMLXXXElement>`
173+
Type representing Props of specified HTML Element - for extending HTML Elements
174+
```tsx
175+
const Input: React.FC<Props & React.HTMLProps<HTMLInputElement>> = props => { ... }
176+
177+
<Input about={...} accept={...} alt={...} ... />
178+
```
179+
180+
#### `React.ReactEventHandler<HTMLXXXElement>`
181+
Type representing generic event handler - for declaring event handlers
174182
```tsx
175183
const handleChange: React.ReactEventHandler<HTMLInputElement> = (ev) => { ... }
176184

177185
<input onChange={handleChange} ... />
178186
```
179187
180-
#### `React.MouseEvent<E>` | `React.KeyboardEvent<E>` | `React.TouchEvent<E>` etc...
181-
Type representing more specific event handler
188+
#### `React.XXXEvent<HTMLXXXElement>`
189+
Type representing more specific event handler. Some common event examples: `ChangeEvent, FormEvent, FocusEvent, KeyboardEvent, MouseEvent, DragEvent, PointerEvent, WheelEvent, TouchEvent`.
182190
```tsx
183191
const handleChange = (ev: React.MouseEvent<HTMLDivElement>) => { ... }
184192

Diff for: README_SOURCE.md

+24-16
Original file line numberDiff line numberDiff line change
@@ -123,33 +123,33 @@ npm i -D @types/react @types/react-dom @types/react-redux
123123

124124
# React - Type-Definitions Cheatsheet
125125

126-
#### `React.FunctionComponent<P>` or `React.FC<P>`
126+
#### `React.FC<Props>` | `React.FunctionComponent<Props>`
127127
Type representing a functional component
128128
```tsx
129129
const MyComponent: React.FC<Props> = ...
130130
```
131131

132-
#### `React.Component<P, S>`
132+
#### `React.Component<Props, State>`
133133
Type representing a class component
134134
```tsx
135135
class MyComponent extends React.Component<Props, State> { ...
136136
```
137137
138-
#### `React.ComponentProps<typeof Component>`
139-
Gets type of Component Props, so you don't need to export Props from your component ever! (Works for both FC and Class components)
140-
```tsx
141-
type MyComponentProps = React.ComponentProps<typeof MyComponent>;
142-
```
143-
144-
#### `React.ComponentType<P>`
145-
Type representing union type of (React.FC | React.Component)
138+
#### `React.ComponentType<Props>`
139+
Type representing union of (React.FC<Props> | React.Component<Props>) - used in HOC
146140
```tsx
147141
const withState = <P extends WrappedComponentProps>(
148142
WrappedComponent: React.ComponentType<P>,
149143
) => { ...
150144
```
151145
152-
#### `React.ReactElement` or `JSX.Element`
146+
#### `React.ComponentProps<typeof XXX>`
147+
Gets Props type of a specified component XXX (WARNING: does not work with statically declared default props and generic props)
148+
```tsx
149+
type MyComponentProps = React.ComponentProps<typeof MyComponent>;
150+
```
151+
152+
#### `React.ReactElement` | `JSX.Element`
153153
Type representing a concept of React Element - representation of a native DOM component (e.g. `<div />`), or a user-defined composite component (e.g. `<MyComponent />`)
154154
```tsx
155155
const elementOnly: React.ReactElement = <div /> || <MyComponent />;
@@ -163,22 +163,30 @@ const Component = ({ children: React.ReactNode }) => ...
163163
```
164164
165165
#### `React.CSSProperties`
166-
Type representing style object in JSX (useful for css-in-js styles)
166+
Type representing style object in JSX - for css-in-js styles
167167
```tsx
168168
const styles: React.CSSProperties = { flexDirection: 'row', ...
169169
const element = <div style={styles} ...
170170
```
171171

172-
#### `React.ReactEventHandler<E>`
173-
Type representing generic event handler
172+
#### `React.HTMLProps<HTMLXXXElement>`
173+
Type representing Props of specified HTML Element - for extending HTML Elements
174+
```tsx
175+
const Input: React.FC<Props & React.HTMLProps<HTMLInputElement>> = props => { ... }
176+
177+
<Input about={...} accept={...} alt={...} ... />
178+
```
179+
180+
#### `React.ReactEventHandler<HTMLXXXElement>`
181+
Type representing generic event handler - for declaring event handlers
174182
```tsx
175183
const handleChange: React.ReactEventHandler<HTMLInputElement> = (ev) => { ... }
176184

177185
<input onChange={handleChange} ... />
178186
```
179187
180-
#### `React.MouseEvent<E>` | `React.KeyboardEvent<E>` | `React.TouchEvent<E>` etc...
181-
Type representing more specific event handler
188+
#### `React.XXXEvent<HTMLXXXElement>`
189+
Type representing more specific event handler. Some common event examples: `ChangeEvent, FormEvent, FocusEvent, KeyboardEvent, MouseEvent, DragEvent, PointerEvent, WheelEvent, TouchEvent`.
182190
```tsx
183191
const handleChange = (ev: React.MouseEvent<HTMLDivElement>) => { ... }
184192

0 commit comments

Comments
 (0)