Skip to content

Commit e721b84

Browse files
feat: created example file
1 parent 41c257a commit e721b84

4 files changed

Lines changed: 103 additions & 36 deletions

File tree

README.md

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -45,63 +45,58 @@ It also requires `useContextUpdate` to behave better in Concurrent Mode.
4545

4646
```tsx
4747
// cats-context.tsx
48-
import { useState } from 'react'
48+
import { useState, FC } from 'react'
4949

5050
import { createContext } from 'react-easy-context-api'
5151

5252
type MyContext = {
53-
pettedCats: number
54-
currentCats: number
53+
pettedCats: number
54+
currentCats: number
5555
}
5656

5757
const { useSelector, Provider } = createContext<MyContext>({
58-
pettedCats: 0
59-
currentCats: 0
58+
pettedCats: 0,
59+
currentCats: 0,
6060
})
6161

62-
const CatsProvider = ({ children }) => {
63-
const [pettedCats, setPettedCats] = useState(0)
64-
const [currentCats, setCurrentCats] = useState(0)
62+
const CatsProvider: FC = ({ children }) => {
63+
const [pettedCats, setPettedCats] = useState(0)
64+
const [currentCats, setCurrentCats] = useState(0)
6565

66-
// ... your context logic :)
66+
// ... your context logic :)
6767

68-
return (
69-
<Provider value={{
70-
pettedCats,
71-
currentCats
72-
}}>
73-
{children}
74-
</Provider>
75-
)
68+
return (
69+
<Provider
70+
value={{
71+
pettedCats,
72+
currentCats,
73+
}}
74+
>
75+
{children}
76+
</Provider>
77+
)
7678
}
7779

78-
export {
79-
CatsProvider,
80-
useSelector
81-
}
80+
export { CatsProvider, useSelector }
8281

8382
// ... in your components
8483
const CatsPetted = () => {
85-
const catsPettedNumber = useSelector(state => state.pettedCats)
84+
const catsPettedNumber = useSelector(state => state.pettedCats)
8685

87-
return (
88-
<p>Petted cats: {catsPettedNumber}</p>
89-
)
86+
return <p>Petted cats: {catsPettedNumber}</p>
9087
}
9188

9289
const CurrentCats = () => {
93-
const currentCatsNumber = useSelector(state => state.currentCats)
90+
const currentCatsNumber = useSelector(state => state.currentCats)
9491

95-
return (
96-
<p>Current cats: {currentCatsNumber}</p>
97-
)
92+
return <p>Current cats: {currentCatsNumber}</p>
9893
}
9994

10095
const App = () => (
101-
<CatsProvider>
102-
<CatsPetted />
103-
<CurrentCats />
104-
</CatsProvider>
96+
<CatsProvider>
97+
<CatsPetted />
98+
<CurrentCats />
99+
</CatsProvider>
105100
)
106101
```
107102

@@ -238,4 +233,4 @@ This hook return a value for BridgeProvider
238233

239234
## Examples
240235

241-
Usage examples are being built.
236+
[See this example file](examples/cats.md).

examples/cats.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
```tsx
2+
import React from 'react'
3+
import ReactDOM from 'react-dom'
4+
// cats-context.tsx
5+
import { useState, FC, Dispatch, SetStateAction } from 'react'
6+
7+
import { createContext } from 'react-easy-context-api'
8+
9+
type MyContext = {
10+
pettedCats: number
11+
currentCats: number
12+
setPettedCats: Dispatch<SetStateAction<number>>
13+
setCurrentCats: Dispatch<SetStateAction<number>>
14+
}
15+
16+
const { useSelector, Provider } = createContext<MyContext>({} as MyContext)
17+
18+
const CatsProvider: FC = ({ children }) => {
19+
const [pettedCats, setPettedCats] = useState(0)
20+
const [currentCats, setCurrentCats] = useState(0)
21+
22+
// ... your context logic :)
23+
24+
return (
25+
<Provider
26+
value={{
27+
pettedCats,
28+
currentCats,
29+
setPettedCats,
30+
setCurrentCats,
31+
}}
32+
>
33+
{children}
34+
</Provider>
35+
)
36+
}
37+
38+
const CatsPetted = () => {
39+
const catsPettedNumber = useSelector(state => state.pettedCats)
40+
const setter = useSelector(state => state.setPettedCats)
41+
42+
return (
43+
<div>
44+
<p>Petted cats: {catsPettedNumber}</p>
45+
<button onClick={() => setter(state => state + 1)}>Add one</button>
46+
</div>
47+
)
48+
}
49+
50+
const CurrentCats = () => {
51+
const currentCatsNumber = useSelector(state => state.currentCats)
52+
const setter = useSelector(state => state.setCurrentCats)
53+
54+
return (
55+
<div>
56+
<p>Current cats: {currentCatsNumber}</p>
57+
<button onClick={() => setter(state => state + 1)}>Add one</button>
58+
</div>
59+
)
60+
}
61+
62+
ReactDOM.render(
63+
<React.StrictMode>
64+
<CatsProvider>
65+
<CatsPetted />
66+
<CurrentCats />
67+
</CatsProvider>
68+
</React.StrictMode>,
69+
document.getElementById('root')
70+
)
71+
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"react",
77
"nexpy"
88
],
9-
"version": "0.0.4-beta",
9+
"version": "0.1.0",
1010
"license": "MIT",
1111
"main": "dist/index.js",
1212
"author": "AllanOliveiraM",

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@
1717
"isolatedModules": true,
1818
"jsx": "react-jsx"
1919
},
20-
"include": ["src/index.tsx"]
20+
"include": ["src/index.tsx"],
21+
"exclude": ["examples/*"]
2122
}

0 commit comments

Comments
 (0)