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`.
52
60
53
-
> CodeSandbox has a [React TypeScript template](https://codesandbox.io/s/react-ts) based on this project.
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)*
54
62
55
63
2.<https://github.com/sw-yx/create-react-app-parcel-typescript> sets up a React + Typescript app with Parcel :)
56
64
3.<https://github.com/basarat/typescript-react/tree/master/01%20bootstrap> for manual setup of React + Typescript + Webpack + Babel
57
65
58
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/).
59
67
60
-
# Import React
68
+
##Import React
61
69
62
70
```tsx
63
71
import*asReactfrom'react';
@@ -81,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.
99
111
100
-
<details>
112
+
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
101
113
102
-
<summary>Explanation</summary>
103
-
104
-
This is not yet written. Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new) with your suggestions!
105
-
</details>
106
-
107
-
# Stateful Class-based Components
114
+
## Stateful Class-based Components
108
115
109
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:
110
117
@@ -180,21 +187,19 @@ class App extends React.Component<{
180
187
}
181
188
}
182
189
```
183
-
<details>
184
190
185
-
<summary>Explanation</summary>
191
+
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
186
192
187
-
This is not yet written. Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new) with your suggestions!
188
-
</details>
193
+
## Typing DefaultProps
189
194
190
-
# 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.
191
196
192
-
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. We have an active discussion on several approaches on how to do this. [Please check out our issue here](https://github.com/sw-yx/react-typescript-cheatsheet/issues/4).
197
+
**We have an active discussion on several approaches on how to do this. [Please check out our issue here](https://github.com/sw-yx/react-typescript-cheatsheet/issues/4).**
193
198
194
199
195
-
# Extracting Prop Types
200
+
##Extracting Prop Types
196
201
197
-
Instead of defining prop types inline, you can declare them separately (useful for reusability or code organization):
202
+
Instead of defining prop types *inline*, you can declare them separately (useful for reusability or code organization):
198
203
199
204
```tsx
200
205
typeAppProps= { message:string }
@@ -224,14 +229,9 @@ class App extends React.Component<AppProps, AppState> {
224
229
225
230
`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)
226
231
227
-
<details>
228
-
229
-
<summary>Explanation</summary>
232
+
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
230
233
231
-
This is not yet written. Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new) with your suggestions!
This is not yet written. Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new) with your suggestions!
278
-
</details>
273
+
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
279
274
280
-
# Forms and Events
275
+
##Forms and Events
281
276
282
277
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:
283
278
@@ -306,14 +301,11 @@ class App extends React.Component<{}, { // no props
306
301
}
307
302
```
308
303
309
-
<details>
310
-
311
-
<summary>Explanation</summary>
304
+
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
312
305
313
-
This is not yet written. Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new) with your suggestions!
314
-
</details>
306
+
# Section 3: Advanced Guides
315
307
316
-
# Higher Order Components/Render Props
308
+
##Higher Order Components/Render Props
317
309
318
310
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:
319
311
@@ -341,14 +333,11 @@ export interface Props {
341
333
}
342
334
```
343
335
344
-
<details>
336
+
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
345
337
346
-
<summary>Explanation</summary>
338
+
## Context
347
339
348
-
This is not yet written. Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new) with your suggestions!
@@ -395,14 +384,9 @@ class Provider extends React.Component<{}, ProviderState> {
395
384
const Consumer =Context.Consumer
396
385
```
397
386
398
-
<details>
399
-
400
-
<summary>Explanation</summary>
401
-
402
-
This is not yet written. Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new) with your suggestions!
403
-
</details>
387
+
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
404
388
405
-
# References/createRef
389
+
## Forwarding References/createRef
406
390
407
391
Use a `React.RefObject`:
408
392
@@ -415,57 +399,51 @@ class CssThemeProvider extends React.PureComponent<Props> {
415
399
}
416
400
```
417
401
418
-
<details>
419
-
420
-
<summary>Explanation</summary>
402
+
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
421
403
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>
404
+
## Portals
424
405
425
-
# Component/Design System Development
406
+
*Not written yet.*
426
407
427
-
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 :)
408
+
[Want to contribute this section? Respond in this issue.](https://github.com/sw-yx/react-typescript-cheatsheet/issues/6)
428
409
429
-
<details>
410
+
## Error Boundaries
430
411
431
-
<summary>Explanation</summary>
412
+
*Not written yet.*
432
413
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>
414
+
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
435
415
436
-
#Building
416
+
## Timeout/Placeholder/createFetcher
437
417
438
-
Please contribute on this topic! [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
418
+
*Not written yet.* watch <https://github.com/sw-yx/fresh-async-react> for more on React Suspense and Time Slicing.
439
419
440
-
<details>
420
+
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
441
421
442
-
<summary>Explanation</summary>
422
+
# Section 4: Misc. Concerns
443
423
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>
424
+
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.
446
425
447
-
#Prettier + TSLint
426
+
## Component/Design System Development
448
427
449
-
Please contribute on this topic! [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
428
+
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 :)
450
429
451
-
<details>
430
+
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
452
431
453
-
<summary>Explanation</summary>
432
+
## Building
454
433
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>
434
+
*Not written yet.*
457
435
458
-
# Working with Non-Typescript Libraries (writing your own index.d.ts)
436
+
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
459
437
460
-
Please contribute on this topic! [We have an ongoing issue here with some references](https://github.com/sw-yx/react-typescript-cheatsheet/issues/8).
438
+
## Prettier + TSLint
461
439
462
-
<details>
440
+
We have an active discussion on Linting [here](https://github.com/sw-yx/react-typescript-cheatsheet/issues/7).
463
441
464
-
<summary>Explanation</summary>
442
+
## Working with Non-Typescript Libraries (writing your own index.d.ts)
465
443
466
-
This is not yet written. Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new) with your suggestions!
467
-
</details>
444
+
*Not written yet.*
468
445
446
+
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