|
16 | 16 | - [Useful React Type Examples](#useful-react-type-examples)
|
17 | 17 | - [Forms and Events](#forms-and-events)
|
18 | 18 | - [Higher Order Components/Render Props](#higher-order-components-render-props)
|
| 19 | +- [Context](#context) |
19 | 20 | - [References/createRef](#references-createref)
|
20 | 21 | - [Component/Design System Development](#component-design-system-development)
|
21 | 22 | - [Building](#building)
|
|
50 | 51 | 1. <https://github.com/wmonk/create-react-app-typescript> is the officially recommended Typescript fork of `create-react-app`.
|
51 | 52 |
|
52 | 53 | > CodeSandbox has a [React TypeScript template](https://codesandbox.io/s/react-ts) based on this project.
|
53 |
| - |
| 54 | +
|
54 | 55 | 2. <https://github.com/sw-yx/create-react-app-parcel-typescript> sets up a React + Typescript app with Parcel :)
|
55 | 56 | 3. <https://github.com/basarat/typescript-react/tree/master/01%20bootstrap> for manual setup of React + Typescript + Webpack + Babel
|
56 | 57 |
|
@@ -127,7 +128,7 @@ class App extends React.Component<{
|
127 | 128 | }, {
|
128 | 129 | count: number, // this is the state type
|
129 | 130 | }> {
|
130 |
| - state = { |
| 131 | + state = { |
131 | 132 | count: 0
|
132 | 133 | }
|
133 | 134 | render() {
|
@@ -347,6 +348,60 @@ export interface Props {
|
347 | 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!
|
348 | 349 | </details>
|
349 | 350 |
|
| 351 | +# Context |
| 352 | + |
| 353 | +Using the new context API `React.createContext`: |
| 354 | + |
| 355 | +```tsx |
| 356 | +interface ProviderState { |
| 357 | + themeColor: string |
| 358 | +} |
| 359 | + |
| 360 | +interface UpdateStateArg { |
| 361 | + key: keyof ProviderState |
| 362 | + value: string |
| 363 | +} |
| 364 | + |
| 365 | +interface ProviderStore { |
| 366 | + state: ProviderState |
| 367 | + update: (arg: UpdateStateArg) => void |
| 368 | +} |
| 369 | + |
| 370 | +const Context = React.createContext({} as ProviderStore) |
| 371 | + |
| 372 | +class Provider extends React.Component<{}, ProviderState> { |
| 373 | + public readonly state = { |
| 374 | + themeColor: 'red' |
| 375 | + } |
| 376 | + |
| 377 | + private update = ({ key, value }: UpdateStateArg) => { |
| 378 | + this.setState({ [key]: value }) |
| 379 | + } |
| 380 | + |
| 381 | + public render() { |
| 382 | + const store: ProviderStore = { |
| 383 | + state: this.state, |
| 384 | + update: this.update |
| 385 | + } |
| 386 | + |
| 387 | + return ( |
| 388 | + <Context.Provider value={store}> |
| 389 | + {this.props.children} |
| 390 | + </Context.Provider> |
| 391 | + ) |
| 392 | + } |
| 393 | +} |
| 394 | + |
| 395 | +const Consumer = Context.Consumer |
| 396 | +``` |
| 397 | + |
| 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> |
| 404 | + |
350 | 405 | # References/createRef
|
351 | 406 |
|
352 | 407 | Use a `React.RefObject`:
|
|
0 commit comments