You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
1.<https://github.com/wmonk/create-react-app-typescript> is the officially recommended Typescript fork of `create-react-app`.
51
60
52
-
> CodeSandbox has a [React TypeScript template](https://codesandbox.io/s/react-ts) based on this project.
53
-
61
+
> CodeSandbox has a [React TypeScript template](https://codesandbox.io/s/react-ts) based on this project.*Contributed by: [@antmdvs](https://github.com/sw-yx/react-typescript-cheatsheet/pull/11)*
62
+
54
63
2.<https://github.com/sw-yx/create-react-app-parcel-typescript> sets up a React + Typescript app with Parcel :)
55
64
3.<https://github.com/basarat/typescript-react/tree/master/01%20bootstrap> for manual setup of React + Typescript + Webpack + Babel
56
65
57
66
In particular, make sure that you have `@types/react` and `@types/react-dom` installed. [Read more about the DefinitelyTyped project if you are unfamiliar](https://definitelytyped.org/).
58
67
59
-
# Import React
68
+
##Import React
60
69
61
70
```tsx
62
71
import*asReactfrom'react';
@@ -80,7 +89,11 @@ Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatshee
Quite frankly I prefer the former pattern as it's shorter.
98
111
99
-
<details>
112
+
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
100
113
101
-
<summary>Explanation</summary>
102
-
103
-
This is not yet written. Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new) with your suggestions!
104
-
</details>
105
-
106
-
# Stateful Class-based Components
114
+
## Stateful Class-based Components
107
115
108
116
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:
109
117
@@ -127,7 +135,7 @@ class App extends React.Component<{
127
135
}, {
128
136
count:number, // this is the state type
129
137
}> {
130
-
state = {
138
+
state = {
131
139
count: 0
132
140
}
133
141
render() {
@@ -179,14 +187,12 @@ class App extends React.Component<{
179
187
}
180
188
}
181
189
```
182
-
<details>
183
190
184
-
<summary>Explanation</summary>
191
+
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
185
192
186
-
This is not yet written. Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new) with your suggestions!
187
-
</details>
193
+
## Typing DefaultProps
188
194
189
-
# Typing DefaultProps
195
+
It is easy to [type defaults for functional components](https://twitter.com/GeeWengel/status/1000242363376205825), but there is some debate in the community on how to type the `static defaultProps` field for class-based components.
190
196
191
197
It is easy to type a defaultProps static member of a React component. There's more than one way to do it, but since we want to show the neatest code as possible
192
198
we choosed to propose this way of implementing them:
@@ -234,10 +240,9 @@ The problem with this approach that if we need to add another prop in the future
234
240
`IMyComponentDefaultProps`!
235
241
</details>
236
242
243
+
## Extracting Prop Types
237
244
238
-
# Extracting Prop Types
239
-
240
-
Instead of defining prop types inline, you can declare them separately (useful for reusability or code organization):
245
+
Instead of defining prop types *inline*, you can declare them separately (useful for reusability or code organization):
241
246
242
247
```tsx
243
248
typeAppProps= { message:string }
@@ -267,14 +272,9 @@ class App extends React.Component<AppProps, AppState> {
267
272
268
273
`interface`s are different from `type`s in Typescript, but for our purposes they do the same things. [read more](https://medium.com/@martin_hotell/interface-vs-type-alias-in-typescript-2-7-2a8f1777af4c)
269
274
270
-
<details>
271
-
272
-
<summary>Explanation</summary>
273
-
274
-
This is not yet written. Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new) with your suggestions!
275
-
</details>
275
+
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
This is not yet written. Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new) with your suggestions!
321
-
</details>
316
+
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
322
317
323
-
# Forms and Events
318
+
##Forms and Events
324
319
325
320
This can be a bit tricky. The tooling really comes in handy here, as the @type definitions come with a wealth of typing. Type what you are looking for and usually the autocomplete will help you out. Here is what it looks like for an `onChange` for a form event:
326
321
@@ -349,14 +344,11 @@ class App extends React.Component<{}, { // no props
349
344
}
350
345
```
351
346
352
-
<details>
347
+
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
353
348
354
-
<summary>Explanation</summary>
355
-
356
-
This is not yet written. Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new) with your suggestions!
357
-
</details>
349
+
# Section 3: Advanced Guides
358
350
359
-
# Higher Order Components/Render Props
351
+
##Higher Order Components/Render Props
360
352
361
353
Sometimes you will want to write a function that can take a React element or a string or something else as a prop. The best Type to use for such a situation is `React.ReactNode` which fits anywhere a normal, well, React Node would fit:
362
354
@@ -384,14 +376,60 @@ export interface Props {
384
376
}
385
377
```
386
378
387
-
<details>
379
+
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
388
380
389
-
<summary>Explanation</summary>
381
+
## Context
390
382
391
-
This is not yet written. Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new) with your suggestions!
private update = ({ key, value }:UpdateStateArg) => {
410
+
this.setState({ [key]: value })
411
+
}
412
+
413
+
public render() {
414
+
const store:ProviderStore= {
415
+
state: this.state,
416
+
update: this.update
417
+
}
418
+
419
+
return (
420
+
<Context.Providervalue={store}>
421
+
{this.props.children}
422
+
</Context.Provider>
423
+
)
424
+
}
425
+
}
426
+
427
+
const Consumer =Context.Consumer
428
+
```
429
+
430
+
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
431
+
432
+
## Forwarding References/createRef
395
433
396
434
Use a `React.RefObject`:
397
435
@@ -404,57 +442,51 @@ class CssThemeProvider extends React.PureComponent<Props> {
404
442
}
405
443
```
406
444
407
-
<details>
445
+
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
408
446
409
-
<summary>Explanation</summary>
447
+
## Portals
410
448
411
-
This is not yet written. Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new) with your suggestions!
412
-
</details>
449
+
*Not written yet.*
413
450
414
-
# Component/Design System Development
451
+
[Want to contribute this section? Respond in this issue.](https://github.com/sw-yx/react-typescript-cheatsheet/issues/6)
415
452
416
-
For developing with Storybook, read the docs I maintain over here: <https://storybook.js.org/configurations/typescript-config/>. This includes automatic proptype documentation generation, which is awesome :)
453
+
## Error Boundaries
417
454
418
-
<details>
419
-
420
-
<summary>Explanation</summary>
455
+
*Not written yet.*
421
456
422
-
This is not yet written. Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new) with your suggestions!
423
-
</details>
457
+
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
424
458
425
-
#Building
459
+
## Timeout/Placeholder/createFetcher
426
460
427
-
Please contribute on this topic! [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
461
+
*Not written yet.* watch <https://github.com/sw-yx/fresh-async-react> for more on React Suspense and Time Slicing.
428
462
429
-
<details>
463
+
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
430
464
431
-
<summary>Explanation</summary>
465
+
# Section 4: Misc. Concerns
432
466
433
-
This is not yet written. Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new) with your suggestions!
434
-
</details>
467
+
Sometimes writing React isn't just about React. While we don't focus on other libraries like Redux (see below for more on that), here are some tips on other common concerns when making apps with React + Typescript.
435
468
436
-
#Prettier + TSLint
469
+
## Component/Design System Development
437
470
438
-
Please contribute on this topic! [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
471
+
For developing with Storybook, read the docs I maintain over here: <https://storybook.js.org/configurations/typescript-config/>. This includes automatic proptype documentation generation, which is awesome :)
439
472
440
-
<details>
473
+
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
441
474
442
-
<summary>Explanation</summary>
475
+
## Building
443
476
444
-
This is not yet written. Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new) with your suggestions!
445
-
</details>
477
+
*Not written yet.*
446
478
447
-
# Working with Non-Typescript Libraries (writing your own index.d.ts)
479
+
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
448
480
449
-
Please contribute on this topic! [We have an ongoing issue here with some references](https://github.com/sw-yx/react-typescript-cheatsheet/issues/8).
481
+
## Prettier + TSLint
450
482
451
-
<details>
483
+
We have an active discussion on Linting [here](https://github.com/sw-yx/react-typescript-cheatsheet/issues/7).
452
484
453
-
<summary>Explanation</summary>
485
+
## Working with Non-Typescript Libraries (writing your own index.d.ts)
454
486
455
-
This is not yet written. Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new) with your suggestions!
456
-
</details>
487
+
*Not written yet.*
457
488
489
+
Please contribute on this topic! [We have an ongoing issue here with some references](https://github.com/sw-yx/react-typescript-cheatsheet/issues/8).
0 commit comments