@webkrafters/react-observable-context 2.1.6 → 2.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +38 -28
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,26 +1,30 @@
|
|
|
1
1
|
# React-Observable-Context
|
|
2
2
|
|
|
3
|
-
A context bearing an observable consumer store.
|
|
3
|
+
A context bearing an observable consumer store.
|
|
4
4
|
|
|
5
|
-
**React
|
|
5
|
+
**Name:** React-Observable-Context
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
**Usage:** Please see [Usage](#usage) section
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
**Install:**\
|
|
10
|
+
npm i -S @webkrafters/react-observable-context\
|
|
11
|
+
npm install --save @webkrafters/react-observable-context
|
|
12
|
+
|
|
13
|
+
# Intro
|
|
10
14
|
|
|
11
|
-
|
|
15
|
+
State changes within the store's internal state are only broadcasted to components subscribed to the store. In this way, the `React-Observable-Context` prevents repeated automatic re-renderings of entire component trees resulting from ***context*** state changes.
|
|
12
16
|
|
|
13
|
-
|
|
17
|
+
**React::memo** *(and PureComponents)* remain the go-to solution for the repeated automatic re-renderings of entire component trees resulting from ***component*** state changes.
|
|
14
18
|
|
|
15
|
-
|
|
19
|
+
_**Recommendation:**_ For optimum performance, consider wrapping in **React::memo** most components using this package's ***useContext*** function either directly or through another React hook. This will protect such components and their descendants from unrelated cascading render operations.
|
|
16
20
|
|
|
17
|
-
|
|
21
|
+
***Exempt*** from the recommendation are certain components such as those wrapped in the `React-Redux::connect()` Higher Order Component (HOC). Such HOC provides similar cascade-render protections to wrapped components and their descendants.
|
|
18
22
|
|
|
19
|
-
|
|
23
|
+
# API
|
|
20
24
|
|
|
21
25
|
The React-Observable-Context package exports **4** modules namely: the **createContext** method, the **useContext** hook, the **Provider** component and the **UsageError** class.
|
|
22
26
|
|
|
23
|
-
* **createContext** is a zero-parameter
|
|
27
|
+
* **createContext** is a zero-parameter function returning a store-bearing context. Pass the context to the useContext() parameter to obtain the context's `store`.
|
|
24
28
|
|
|
25
29
|
* **useContext** is analogous to React::useContext hook but returns the context store and takes a second parameter named ***watchedKeys***. The `watchedKeys` parameter is a list of state object property paths to watch. A change in any of the referenced properties automatically triggers a render of the component calling this hook.
|
|
26
30
|
|
|
@@ -30,7 +34,7 @@ The React-Observable-Context package exports **4** modules namely: the **createC
|
|
|
30
34
|
|
|
31
35
|
***<u>Note:</u>*** the Provider `context` prop is not updateable. Once set, all further updates to this prop are not recorded.
|
|
32
36
|
|
|
33
|
-
|
|
37
|
+
## The Store
|
|
34
38
|
|
|
35
39
|
The context's `store` exposes **4** methods for interacting with the context's internal state namely:
|
|
36
40
|
|
|
@@ -38,23 +42,29 @@ The context's `store` exposes **4** methods for interacting with the context's i
|
|
|
38
42
|
|
|
39
43
|
* **resetState**: VoidFunction // resets the state to the Provider initial `value` prop.
|
|
40
44
|
|
|
41
|
-
* **setState**: (changes: PartialState\<State\>) => void // sets only new/changed
|
|
45
|
+
* **setState**: (changes: PartialState\<State\>) => void // sets only new/changed state slices.\
|
|
46
|
+
***Do this:*** `setState({stateKey0: changes0[, ...]});`\
|
|
47
|
+
***Not this:*** `setState({stateKey0: {...state.stateKey0, ...changes0}[, ...]});`
|
|
42
48
|
|
|
43
49
|
* **subscribe**: (listener: (newValue: PartialState\<State\>, oldValue: PartialState\<State\>) => void) => ***UnsubscribeFunction***
|
|
44
50
|
|
|
45
|
-
|
|
51
|
+
## Prehooks
|
|
52
|
+
|
|
53
|
+
Prehooks provide a central place for sanitizing, modifying, transforming, validating etc. all related incoming state updates.
|
|
46
54
|
|
|
47
|
-
The context
|
|
55
|
+
The context store **2** update operations each adhere to its own user specified prehook when present. Otherwise, the update operation proceeds normally to completion. Thus, there are **2** prehooks named **resetState** and **setState** - after the store update methods they support.
|
|
56
|
+
|
|
57
|
+
Each prehook returns a **boolean** value (`true` to continue AND `false` to abort the update operation). The prehook may modify (i.e. sanitize, transform, transpose) the argument to accurately reflect the intended update value. This is done by mutating part of the argument which holds the next `nextUpdate` values.
|
|
48
58
|
|
|
49
|
-
* **resetState**: (state: {current: State, original: State}) => boolean
|
|
59
|
+
* **resetState**: (state: {current: State, original: State}) => boolean // ***`state.original`*** holds the `nextUpdate` values.
|
|
50
60
|
|
|
51
|
-
* **setState**: (newChanges: PartialState\<State\>) => boolean
|
|
61
|
+
* **setState**: (newChanges: PartialState\<State\>) => boolean // ***`newChanges`*** holds the `nextUpdate` values.
|
|
52
62
|
|
|
53
|
-
***<u>Use case:</u>*** prehooks provide a central place for sanitizing, modifying, transforming, validating etc. all related incoming state updates.
|
|
63
|
+
***<u>Use case:</u>*** prehooks provide a central place for sanitizing, modifying, transforming, validating etc. all related incoming state updates.
|
|
54
64
|
|
|
55
|
-
|
|
65
|
+
# Usage
|
|
56
66
|
|
|
57
|
-
<
|
|
67
|
+
### <u>*context.js*</u>
|
|
58
68
|
|
|
59
69
|
import React from 'react';
|
|
60
70
|
|
|
@@ -81,14 +91,14 @@ The context's store update operation adheres to **2** user supplied prehooks whe
|
|
|
81
91
|
|
|
82
92
|
export default ObservableContext;
|
|
83
93
|
|
|
84
|
-
<
|
|
94
|
+
### <u>*index.js*</u>
|
|
85
95
|
|
|
86
96
|
import React from 'react';
|
|
87
97
|
import ReactDOM from 'react-dom';
|
|
88
98
|
import App from './app';
|
|
89
99
|
ReactDOM.render(<App />, document.getElementById('root'));
|
|
90
100
|
|
|
91
|
-
<
|
|
101
|
+
### <u>*app.js*</u>
|
|
92
102
|
|
|
93
103
|
import React, { useCallback, useState } from 'react';
|
|
94
104
|
import Product from './product';
|
|
@@ -122,7 +132,7 @@ The context's store update operation adheres to **2** user supplied prehooks whe
|
|
|
122
132
|
App.displayName = 'App';
|
|
123
133
|
export default App;
|
|
124
134
|
|
|
125
|
-
<
|
|
135
|
+
### <u>*product.js*</u>
|
|
126
136
|
|
|
127
137
|
import React, { useCallback, useEffect, useState } from 'react';
|
|
128
138
|
import { ObservableContextProvider } from './context';
|
|
@@ -167,7 +177,7 @@ The context's store update operation adheres to **2** user supplied prehooks whe
|
|
|
167
177
|
Product.displayName = 'Product';
|
|
168
178
|
export default Product;
|
|
169
179
|
|
|
170
|
-
<
|
|
180
|
+
### <u>*editor.js*</u>
|
|
171
181
|
|
|
172
182
|
import React, { memo, useCallback, useEffect, useRef } from 'react';
|
|
173
183
|
import { useObservableContext } from './context';
|
|
@@ -210,7 +220,7 @@ The context's store update operation adheres to **2** user supplied prehooks whe
|
|
|
210
220
|
Editor.displayName = 'Editor';
|
|
211
221
|
export default Editor;
|
|
212
222
|
|
|
213
|
-
<
|
|
223
|
+
### <u>*price-sticker.js*</u>
|
|
214
224
|
|
|
215
225
|
import React, { memo, useEffect } from 'react';
|
|
216
226
|
import { useObservableContext } from './context';
|
|
@@ -227,7 +237,7 @@ The context's store update operation adheres to **2** user supplied prehooks whe
|
|
|
227
237
|
PriceSticker.displayName = 'PriceSticker';
|
|
228
238
|
export default PriceSticker;
|
|
229
239
|
|
|
230
|
-
<
|
|
240
|
+
### <u>*product-description.js*</u>
|
|
231
241
|
|
|
232
242
|
import React, { memo, useEffect } from 'react';
|
|
233
243
|
import { useObservableContext } from './context';
|
|
@@ -246,7 +256,7 @@ The context's store update operation adheres to **2** user supplied prehooks whe
|
|
|
246
256
|
ProductDescription.displayName = 'ProductDescription';
|
|
247
257
|
export default ProductDescription;
|
|
248
258
|
|
|
249
|
-
<
|
|
259
|
+
### <u>*tally-display.js*</u>
|
|
250
260
|
|
|
251
261
|
import React, { memo, useEffect } from 'react';
|
|
252
262
|
import { useObservableContext } from './context';
|
|
@@ -273,7 +283,7 @@ The context's store update operation adheres to **2** user supplied prehooks whe
|
|
|
273
283
|
TallyDisplay.displayName = 'TallyDisplay';
|
|
274
284
|
export default TallyDisplay;
|
|
275
285
|
|
|
276
|
-
<
|
|
286
|
+
### <u>*reset.js*</u>
|
|
277
287
|
|
|
278
288
|
import React, { memo, useEffect } from 'react';
|
|
279
289
|
import { useObservableContext } from './context';
|
|
@@ -286,6 +296,6 @@ The context's store update operation adheres to **2** user supplied prehooks whe
|
|
|
286
296
|
export default Reset;
|
|
287
297
|
|
|
288
298
|
|
|
289
|
-
|
|
299
|
+
# License
|
|
290
300
|
|
|
291
301
|
MIT
|
package/package.json
CHANGED