Skip to content

Commit a4310a8

Browse files
authored
Added `React.ComponentProps<typeof Component> (piotrwitek#132)
Added missing quote Solved piotrwitek#117
1 parent ac58144 commit a4310a8

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,48 +99,48 @@ npm i -D @types/react @types/react-dom @types/react-redux
9999
#### `React.FunctionComponent<P>` or `React.FC<P>`
100100
Type representing a functional component
101101
```tsx
102-
const MyComponent: React.FC<MyComponentProps> = ...
102+
const MyComponent: React.FC<Props> = ...
103103
```
104-
[⇧ back to top](#table-of-contents)
105104

106105
#### `React.Component<P, S>`
107106
Type representing a class component
108107
```tsx
109-
class MyComponent extends React.Component<MyComponentProps, State> { ...
108+
class MyComponent extends React.Component<Props, State> { ...
109+
```
110+
111+
#### `React.ComponentProps<typeof Component>`
112+
Gets type of Component Props, so you don't need to export Props from your component ever! (Works for both FC and Class components)
113+
```tsx
114+
type MyComponentProps = React.ComponentProps<typeof MyComponent>;
110115
```
111-
[⇧ back to top](#table-of-contents)
112116
113117
#### `React.ComponentType<P>`
114-
Type representing union type of (FC | Component)
118+
Type representing union type of (React.FC | React.Component)
115119
```tsx
116120
const withState = <P extends WrappedComponentProps>(
117121
WrappedComponent: React.ComponentType<P>,
118122
) => { ...
119123
```
120-
[⇧ back to top](#table-of-contents)
121124
122125
#### `React.ReactElement<P>` or `JSX.Element`
123126
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 />`)
124127
```tsx
125128
const elementOnly: React.ReactElement = <div /> || <MyComponent />;
126129
```
127-
[⇧ back to top](#table-of-contents)
128130
129131
#### `React.ReactNode`
130132
Type representing any possible type of React node (basically ReactElement (including Fragments and Portals) + primitive JS types)
131133
```tsx
132134
const elementOrPrimitive: React.ReactNode = 'string' || 0 || false || null || undefined || <div /> || <MyComponent />;
133135
const Component = ({ children: React.ReactNode }) => ...
134136
```
135-
[⇧ back to top](#table-of-contents)
136137
137138
#### `React.CSSProperties`
138139
Type representing style object in JSX (usefull for css-in-js styles)
139140
```tsx
140141
const styles: React.CSSProperties = { flexDirection: 'row', ...
141142
const element = <div style={styles} ...
142143
```
143-
[⇧ back to top](#table-of-contents)
144144

145145
#### `React.ReactEventHandler<E>`
146146
Type representing generic event handler
@@ -149,7 +149,6 @@ const handleChange: React.ReactEventHandler<HTMLInputElement> = (ev) => { ... }
149149

150150
<input onChange={handleChange} ... />
151151
```
152-
[⇧ back to top](#table-of-contents)
153152
154153
#### `React.MouseEvent<E>` | `React.KeyboardEvent<E>` | `React.TouchEvent<E>` etc...
155154
Type representing more specific event handler
@@ -158,6 +157,7 @@ const handleChange = (ev: React.MouseEvent<HTMLDivElement>) => { ... }
158157

159158
<div onMouseMove={handleChange} ... />
160159
```
160+
161161
[⇧ back to top](#table-of-contents)
162162
163163
---

README_SOURCE.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,48 +99,48 @@ npm i -D @types/react @types/react-dom @types/react-redux
9999
#### `React.FunctionComponent<P>` or `React.FC<P>`
100100
Type representing a functional component
101101
```tsx
102-
const MyComponent: React.FC<MyComponentProps> = ...
102+
const MyComponent: React.FC<Props> = ...
103103
```
104-
[⇧ back to top](#table-of-contents)
105104

106105
#### `React.Component<P, S>`
107106
Type representing a class component
108107
```tsx
109-
class MyComponent extends React.Component<MyComponentProps, State> { ...
108+
class MyComponent extends React.Component<Props, State> { ...
109+
```
110+
111+
#### `React.ComponentProps<typeof Component>`
112+
Gets type of Component Props, so you don't need to export Props from your component ever! (Works for both FC and Class components)
113+
```tsx
114+
type MyComponentProps = React.ComponentProps<typeof MyComponent>;
110115
```
111-
[⇧ back to top](#table-of-contents)
112116
113117
#### `React.ComponentType<P>`
114-
Type representing union type of (FC | Component)
118+
Type representing union type of (React.FC | React.Component)
115119
```tsx
116120
const withState = <P extends WrappedComponentProps>(
117121
WrappedComponent: React.ComponentType<P>,
118122
) => { ...
119123
```
120-
[⇧ back to top](#table-of-contents)
121124
122125
#### `React.ReactElement<P>` or `JSX.Element`
123126
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 />`)
124127
```tsx
125128
const elementOnly: React.ReactElement = <div /> || <MyComponent />;
126129
```
127-
[⇧ back to top](#table-of-contents)
128130
129131
#### `React.ReactNode`
130132
Type representing any possible type of React node (basically ReactElement (including Fragments and Portals) + primitive JS types)
131133
```tsx
132134
const elementOrPrimitive: React.ReactNode = 'string' || 0 || false || null || undefined || <div /> || <MyComponent />;
133135
const Component = ({ children: React.ReactNode }) => ...
134136
```
135-
[⇧ back to top](#table-of-contents)
136137
137138
#### `React.CSSProperties`
138139
Type representing style object in JSX (usefull for css-in-js styles)
139140
```tsx
140141
const styles: React.CSSProperties = { flexDirection: 'row', ...
141142
const element = <div style={styles} ...
142143
```
143-
[⇧ back to top](#table-of-contents)
144144

145145
#### `React.ReactEventHandler<E>`
146146
Type representing generic event handler
@@ -149,7 +149,6 @@ const handleChange: React.ReactEventHandler<HTMLInputElement> = (ev) => { ... }
149149

150150
<input onChange={handleChange} ... />
151151
```
152-
[⇧ back to top](#table-of-contents)
153152
154153
#### `React.MouseEvent<E>` | `React.KeyboardEvent<E>` | `React.TouchEvent<E>` etc...
155154
Type representing more specific event handler
@@ -158,6 +157,7 @@ const handleChange = (ev: React.MouseEvent<HTMLDivElement>) => { ... }
158157

159158
<div onMouseMove={handleChange} ... />
160159
```
160+
161161
[⇧ back to top](#table-of-contents)
162162
163163
---

0 commit comments

Comments
 (0)