File tree Expand file tree Collapse file tree 2 files changed +87
-0
lines changed
src/components/AppContext Expand file tree Collapse file tree 2 files changed +87
-0
lines changed Original file line number Diff line number Diff line change @@ -35,3 +35,10 @@ and the "Select File" icon to set/change the file.
3535Use 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 )
Original file line number Diff line number Diff line change 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+ ```
You can’t perform that action at this time.
0 commit comments