Skip to content

Commit 326f63c

Browse files
authored
Merge pull request #13 from jpavon/patch/context
this is great! thank you! I might have to reorganize a bit, i'll do this after merge
2 parents 5b81f3c + 00cff4a commit 326f63c

File tree

1 file changed

+57
-2
lines changed

1 file changed

+57
-2
lines changed

README.md

+57-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- [Useful React Type Examples](#useful-react-type-examples)
1717
- [Forms and Events](#forms-and-events)
1818
- [Higher Order Components/Render Props](#higher-order-components-render-props)
19+
- [Context](#context)
1920
- [References/createRef](#references-createref)
2021
- [Component/Design System Development](#component-design-system-development)
2122
- [Building](#building)
@@ -50,7 +51,7 @@
5051
1. <https://github.com/wmonk/create-react-app-typescript> is the officially recommended Typescript fork of `create-react-app`.
5152

5253
> CodeSandbox has a [React TypeScript template](https://codesandbox.io/s/react-ts) based on this project.
53-
54+
5455
2. <https://github.com/sw-yx/create-react-app-parcel-typescript> sets up a React + Typescript app with Parcel :)
5556
3. <https://github.com/basarat/typescript-react/tree/master/01%20bootstrap> for manual setup of React + Typescript + Webpack + Babel
5657

@@ -127,7 +128,7 @@ class App extends React.Component<{
127128
}, {
128129
count: number, // this is the state type
129130
}> {
130-
state = {
131+
state = {
131132
count: 0
132133
}
133134
render() {
@@ -347,6 +348,60 @@ export interface Props {
347348
This is not yet written. Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new) with your suggestions!
348349
</details>
349350

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+
350405
# References/createRef
351406

352407
Use a `React.RefObject`:

0 commit comments

Comments
 (0)