Skip to content

Commit b2497e8

Browse files
committed
edited README
1 parent 4d398e3 commit b2497e8

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,10 @@ and the "Select File" icon to set/change the file.
3535
Use React's `useEffect` hook asynchronously (safely).
3636

3737
![useAsyncEffect](./src/hooks/useAsyncEffect/screenshot.gif)
38+
39+
40+
### [AppContext](./src/components/AppContext/README.md)
41+
42+
A Context Provider that can be consumed with a `useAppContext()` hook, updating its props (thus re-rendering) independently.
43+
44+
Check out the [example](./src/components/AppContext/example/index.tsx)
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
2+
### AppContext
3+
4+
A Context Provider that can be consumed with a `useAppContext()` hook, updating its props (thus re-rendering) independently.
5+
6+
For a more thorough example: [./example/index.tsx](./example/index.tsx)
7+
8+
### Usage:
9+
10+
```typescript
11+
import { useMemo } from 'react'
12+
import {
13+
AppProvider,
14+
useAppContext,
15+
} from '@emmveqz/react-components/components/AppProvider'
16+
17+
// This is a sample context.
18+
type IAppContext = {
19+
app: {
20+
language: number
21+
},
22+
theme: {
23+
color: string,
24+
},
25+
}
26+
27+
export const Root = () => {
28+
const props = useMemo<IAppContext>(() => ({
29+
app: {
30+
language: 1,
31+
},
32+
theme: {
33+
color: `blue ${Date.now()}`,
34+
},
35+
}), [
36+
])
37+
38+
return (
39+
<AppProvider props={props}>
40+
<App />
41+
</AppProvider>
42+
)
43+
}
44+
45+
export const App = () => {
46+
return (
47+
<div>
48+
<UpdateProps />
49+
<SomeOtherComponent />
50+
</div>
51+
)
52+
}
53+
54+
export const UpdateProps = () => {
55+
const appContext = useAppContext<IAppContext>()
56+
57+
const [
58+
app,
59+
setApp,
60+
] = appContext.app()
61+
62+
const updateProps = () => {
63+
setApp({
64+
language: app.language + 1,
65+
})
66+
}
67+
68+
return (
69+
<div>
70+
<div>app.language: {app.language}</div>
71+
72+
<div>
73+
<button type="button" onClick={updateProps}>
74+
update app.language
75+
</button>
76+
</div>
77+
</div>
78+
)
79+
}
80+
```

0 commit comments

Comments
 (0)