Skip to content

Commit f132845

Browse files
feat: version 1.0.2 done - docs and exports upgrade
1 parent 006a69c commit f132845

4 files changed

Lines changed: 133 additions & 65 deletions

File tree

README.md

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ To make it work like original React context, it uses
4141
It also requires `useContextUpdate` to behave better in Concurrent Mode.
4242
(You don't need to use it in Legacy Mode.)
4343

44+
<br />
45+
4446
## Usage
4547

4648
```tsx
@@ -100,15 +102,19 @@ const App = () => (
100102
)
101103
```
102104

105+
---
106+
103107
## API
104108

105-
### createContext
109+
<br />
110+
111+
### **`createContext`**
106112

107113
This creates a special context.
108114

109115
#### Parameters
110116

111-
- `defaultValue` **Value**
117+
- `defaultValue`: **Value**
112118

113119
#### Examples
114120

@@ -123,9 +129,13 @@ type PersonContext = {
123129
const PersonContext = createContext<PersonContext>({ firstName: '', familyName: '' })
124130
```
125131

126-
### Returns:
132+
<br />
133+
134+
### **createContext Returns:**
127135

128-
#### useSelector
136+
<br />
137+
138+
#### `useSelector`
129139

130140
This hook returns context selected value by selector.
131141

@@ -135,7 +145,7 @@ The selector should return referentially equal result for same input for better
135145

136146
##### Parameters
137147

138-
- `selector` **function (value: Value): Selected**
148+
- `selector`: **function (value: Value): Selected**
139149

140150
##### Examples
141151

@@ -147,7 +157,9 @@ const MyComponent = () => {
147157
}
148158
```
149159

150-
#### useContext
160+
<br />
161+
162+
#### `useContext`
151163

152164
This hook returns the entire context value.
153165
Use this instead of React.useContext for consistent behavior.
@@ -161,7 +173,9 @@ const MyComponent = () => {
161173
}
162174
```
163175

164-
#### useContextUpdate
176+
<br />
177+
178+
#### `useContextUpdate`
165179

166180
This hook returns an update function that accepts a thunk function.
167181

@@ -180,17 +194,43 @@ const MyComponent = () => {
180194
}
181195
```
182196

183-
#### BridgeProvider
197+
<br />
184198

185-
This is a Provider component for bridging multiple react roots.
199+
#### `Provider`
200+
201+
The provider you need to use to apply the context.
186202

187-
#### Context
203+
---
188204

189-
The special context created by createContext hook.
205+
<br />
190206

191-
#### Provider
207+
### **Exotic Returns:**
192208

193-
The provider you need to use to apply the context.
209+
You probably won't need the following features. Use only if you know what you are doing.
210+
211+
<br />
212+
213+
#### `Context`
214+
215+
The special context created by createContext hook. This context should not be consumed by the ro react `useContext` API, but by `useSpecialContext` bellow.
216+
217+
<br />
218+
219+
#### `useSpecialContext`
220+
221+
This Hook is used to manually consume the same special context `Context` created by `createContext` function. This is not necessary as there is a level of abstraction on top of that and this hook should only be used with contexts created by the library. This utility is only available so there are no limitations on the use of this library.
222+
223+
<br />
224+
225+
#### `useBridgeValue`
226+
227+
This hook return a value for BridgeProvider.
228+
229+
<br />
230+
231+
#### `BridgeProvider`
232+
233+
This is a Provider component for bridging multiple react roots.
194234

195235
##### Parameters
196236

@@ -227,9 +267,7 @@ const App = () => {
227267
}
228268
```
229269

230-
#### useBridgeValue
231-
232-
This hook return a value for BridgeProvider.
270+
---
233271

234272
## Limitations
235273

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"context",
99
"hooks"
1010
],
11-
"version": "1.0.1",
11+
"version": "1.0.2",
1212
"license": "MIT",
1313
"author": "AllanOliveiraM",
1414
"main": "./dist/index.js",
@@ -19,6 +19,7 @@
1919
"husky:install": "husky install",
2020
"precompile:native": ". automation/precompile-native.sh",
2121
"build": "yarn --silent precompile:native && rollup -c",
22+
"build:complete": "rm -rf ./dist && yarn precompile:native && yarn build",
2223
"start": "yarn --silent precompile:native && rollup -c -w",
2324
"commit": "npx git-cz",
2425
"type-check": "tsc --project tsconfig.json --pretty --noEmit",

src/index.tsx

Lines changed: 76 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -29,76 +29,104 @@ type ProviderProps<Value> = PropsWithChildren<{
2929
* const PersonContext = createContext<PersonContext>({ firstName: '', familyName: '' })
3030
*/
3131
const createContext = <Value,>(defaultValue: Value) => {
32-
/**
33-
* The special context created by createContext hook.
34-
*/
3532
const Context = createContextWithSelector(defaultValue)
3633

37-
/**
38-
* This hook returns context selected value by selector.
39-
* It will trigger re-render if only the selected value is referentially changed.
40-
* The selector should return referentially equal result for same input for better performance.
41-
*
42-
* @param selector function (value: Value): Selected
43-
*
44-
* @example
45-
* const MyComponent = () => {
46-
* const firstName = PersonContext.useSelector(state => state.firstName)
47-
*
48-
* return <p>{firstName}</p>
49-
* }
50-
*/
5134
const useSelector = <Selected,>(selector: (state: Value) => Selected) =>
5235
useContextSelector(Context, selector)
5336

54-
/**
55-
* This hook returns the entire context value.
56-
* Use this instead of React.useContext for consistent behavior.
57-
*
58-
* @example
59-
* const MyComponent = () => {
60-
* const person = PersonContext.useContext()
61-
* ...
62-
* }
63-
*/
6437
const useContext = () => useSpecialContext(Context)
6538

66-
/**
67-
* This hook returns an update function that accepts a thunk function.
68-
*
69-
* Use this for a function that will change a value in
70-
* [Concurrent Mode](https://reactjs.org/docs/concurrent-mode-intro.html).
71-
* Otherwise, there's no need to use this hook.
72-
*
73-
* @example
74-
* const MyComponent = () => {
75-
* const update = PersonContext.useContextUpdate()
76-
*
77-
* update(() => setState(...));
78-
* ...
79-
* }
80-
*/
8139
const useContextUpdate = () => useSpecialContextUpdate(Context)
8240

83-
/**
84-
* This hook return a value for BridgeProvider.
85-
*/
8641
const useBridgeValue = () => useSpecialBridgeValue(Context)
8742

88-
/**
89-
* The provider you need to use to apply the context.
90-
*/
9143
const Provider = ({ children, value }: ProviderProps<Value>) => (
9244
<Context.Provider value={value}>{children}</Context.Provider>
9345
)
9446

9547
return {
48+
/**
49+
* This Hook is used to manually consume the same special context
50+
* `Context` created by `createContext` function. This is not necessary
51+
* as there is a level of abstraction on top of that and this hook should
52+
* only be used with contexts created by the library. This utility is only
53+
* available so there are no limitations on the use of this library.
54+
*/
55+
useSpecialContext,
56+
57+
/**
58+
* The special context created by createContext hook.
59+
*/
9660
Context,
61+
62+
/**
63+
* This hook returns context selected value by selector.
64+
* It will trigger re-render if only the selected value is referentially changed.
65+
* The selector should return referentially equal result for same input for better performance.
66+
*
67+
* @param selector function (value: Value): Selected
68+
*
69+
* @example
70+
* const MyComponent = () => {
71+
* const firstName = PersonContext.useSelector(state => state.firstName)
72+
*
73+
* return <p>{firstName}</p>
74+
* }
75+
*/
9776
useSelector,
77+
78+
/**
79+
* This hook returns the entire context value.
80+
* Use this instead of React.useContext for consistent behavior.
81+
*
82+
* @example
83+
* const MyComponent = () => {
84+
* const person = PersonContext.useContext()
85+
* ...
86+
* }
87+
*/
9888
useContext,
89+
90+
/**
91+
* This hook returns an update function that accepts a thunk function.
92+
*
93+
* Use this for a function that will change a value in
94+
* [Concurrent Mode](https://reactjs.org/docs/concurrent-mode-intro.html).
95+
* Otherwise, there's no need to use this hook.
96+
*
97+
* @example
98+
* const MyComponent = () => {
99+
* const update = PersonContext.useContextUpdate()
100+
*
101+
* update(() => setState(...));
102+
* ...
103+
* }
104+
*/
99105
useContextUpdate,
106+
107+
/**
108+
* This hook return a value for BridgeProvider.
109+
*/
100110
useBridgeValue,
111+
112+
/**
113+
* The provider you need to use to apply the context.
114+
*/
101115
Provider,
116+
117+
/**
118+
* This is a Provider component for bridging multiple react roots
119+
*
120+
* @example
121+
* const valueToBridge = useBridgeValue(PersonContext);
122+
* return (
123+
* <Renderer>
124+
* <BridgeProvider context={Context} value={valueToBridge}>
125+
* {children}
126+
* </BridgeProvider>
127+
* </Renderer>
128+
* );
129+
*/
102130
BridgeProvider,
103131
}
104132
}

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"moduleResolution": "node",
1616
"resolveJsonModule": true,
1717
"isolatedModules": true,
18+
"removeComments": false,
1819
"jsx": "react-jsx"
1920
},
2021
"include": ["src/**/*.ts*"],

0 commit comments

Comments
 (0)