@webkrafters/react-observable-context 2.1.7 → 2.1.9
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 +31 -27
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,36 +1,40 @@
|
|
|
1
1
|
# React-Observable-Context
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Only re-renders subscribing components on context state change. A subscribing component decides which context state properties' change trigger its update.
|
|
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
|
|
10
12
|
|
|
11
|
-
|
|
13
|
+
# Intro
|
|
12
14
|
|
|
13
|
-
|
|
15
|
+
A context bearing an observable consumer [store](#store). 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.
|
|
14
16
|
|
|
15
|
-
|
|
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.
|
|
16
18
|
|
|
17
|
-
|
|
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.
|
|
20
|
+
|
|
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 as a `context` parameter to the [useContext()](#usecontext) to obtain the context's [store](#store).
|
|
24
28
|
|
|
25
|
-
*
|
|
29
|
+
* <b id="usecontext">useContext</b> 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
|
|
|
27
|
-
* **Provider** can immediately be used as-is anywhere the React-Observable-Context is required. It accepts **3** props and the customary Provider `children` prop. Supply the context to its `context` prop; the initial state to the customary Provider `value` prop; and the optional `prehooks` prop <i>(discussed in the prehooks section below)</i>.
|
|
31
|
+
* **Provider** can immediately be used as-is anywhere the React-Observable-Context is required. It accepts **3** props and the customary Provider `children` prop. Supply the context to its `context` prop; the initial state to the customary Provider `value` prop; and the optional `prehooks` prop <i>(discussed in the [prehooks](#prehooks) section below)</i>.
|
|
28
32
|
|
|
29
33
|
* **UsageError** class is the Error type reported for attempts to access this context's store outside of its Provider component tree.
|
|
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
|
+
## Store
|
|
34
38
|
|
|
35
39
|
The context's `store` exposes **4** methods for interacting with the context's internal state namely:
|
|
36
40
|
|
|
@@ -39,12 +43,12 @@ The context's `store` exposes **4** methods for interacting with the context's i
|
|
|
39
43
|
* **resetState**: VoidFunction // resets the state to the Provider initial `value` prop.
|
|
40
44
|
|
|
41
45
|
* **setState**: (changes: PartialState\<State\>) => void // sets only new/changed state slices.\
|
|
42
|
-
***Do this:***
|
|
43
|
-
***Not this:***
|
|
46
|
+
***Do this:*** <code style="color:#080">setState({stateKey0: changes0[, ...]});</code>\
|
|
47
|
+
***Not this:*** <code style="color:#800">setState({stateKey0: {...state.stateKey0, ...changes0}[, ...]});</code>
|
|
44
48
|
|
|
45
49
|
* **subscribe**: (listener: (newValue: PartialState\<State\>, oldValue: PartialState\<State\>) => void) => ***UnsubscribeFunction***
|
|
46
50
|
|
|
47
|
-
|
|
51
|
+
## Prehooks
|
|
48
52
|
|
|
49
53
|
Prehooks provide a central place for sanitizing, modifying, transforming, validating etc. all related incoming state updates.
|
|
50
54
|
|
|
@@ -58,9 +62,9 @@ The context's `store` exposes **4** methods for interacting with the context's i
|
|
|
58
62
|
|
|
59
63
|
***<u>Use case:</u>*** prehooks provide a central place for sanitizing, modifying, transforming, validating etc. all related incoming state updates.
|
|
60
64
|
|
|
61
|
-
|
|
65
|
+
# Usage
|
|
62
66
|
|
|
63
|
-
<
|
|
67
|
+
### <u>*context.js*</u>
|
|
64
68
|
|
|
65
69
|
import React from 'react';
|
|
66
70
|
|
|
@@ -87,14 +91,14 @@ The context's `store` exposes **4** methods for interacting with the context's i
|
|
|
87
91
|
|
|
88
92
|
export default ObservableContext;
|
|
89
93
|
|
|
90
|
-
<
|
|
94
|
+
### <u>*index.js*</u>
|
|
91
95
|
|
|
92
96
|
import React from 'react';
|
|
93
97
|
import ReactDOM from 'react-dom';
|
|
94
98
|
import App from './app';
|
|
95
99
|
ReactDOM.render(<App />, document.getElementById('root'));
|
|
96
100
|
|
|
97
|
-
<
|
|
101
|
+
### <u>*app.js*</u>
|
|
98
102
|
|
|
99
103
|
import React, { useCallback, useState } from 'react';
|
|
100
104
|
import Product from './product';
|
|
@@ -128,7 +132,7 @@ The context's `store` exposes **4** methods for interacting with the context's i
|
|
|
128
132
|
App.displayName = 'App';
|
|
129
133
|
export default App;
|
|
130
134
|
|
|
131
|
-
<
|
|
135
|
+
### <u>*product.js*</u>
|
|
132
136
|
|
|
133
137
|
import React, { useCallback, useEffect, useState } from 'react';
|
|
134
138
|
import { ObservableContextProvider } from './context';
|
|
@@ -173,7 +177,7 @@ The context's `store` exposes **4** methods for interacting with the context's i
|
|
|
173
177
|
Product.displayName = 'Product';
|
|
174
178
|
export default Product;
|
|
175
179
|
|
|
176
|
-
<
|
|
180
|
+
### <u>*editor.js*</u>
|
|
177
181
|
|
|
178
182
|
import React, { memo, useCallback, useEffect, useRef } from 'react';
|
|
179
183
|
import { useObservableContext } from './context';
|
|
@@ -216,7 +220,7 @@ The context's `store` exposes **4** methods for interacting with the context's i
|
|
|
216
220
|
Editor.displayName = 'Editor';
|
|
217
221
|
export default Editor;
|
|
218
222
|
|
|
219
|
-
<
|
|
223
|
+
### <u>*price-sticker.js*</u>
|
|
220
224
|
|
|
221
225
|
import React, { memo, useEffect } from 'react';
|
|
222
226
|
import { useObservableContext } from './context';
|
|
@@ -233,7 +237,7 @@ The context's `store` exposes **4** methods for interacting with the context's i
|
|
|
233
237
|
PriceSticker.displayName = 'PriceSticker';
|
|
234
238
|
export default PriceSticker;
|
|
235
239
|
|
|
236
|
-
<
|
|
240
|
+
### <u>*product-description.js*</u>
|
|
237
241
|
|
|
238
242
|
import React, { memo, useEffect } from 'react';
|
|
239
243
|
import { useObservableContext } from './context';
|
|
@@ -252,7 +256,7 @@ The context's `store` exposes **4** methods for interacting with the context's i
|
|
|
252
256
|
ProductDescription.displayName = 'ProductDescription';
|
|
253
257
|
export default ProductDescription;
|
|
254
258
|
|
|
255
|
-
<
|
|
259
|
+
### <u>*tally-display.js*</u>
|
|
256
260
|
|
|
257
261
|
import React, { memo, useEffect } from 'react';
|
|
258
262
|
import { useObservableContext } from './context';
|
|
@@ -279,7 +283,7 @@ The context's `store` exposes **4** methods for interacting with the context's i
|
|
|
279
283
|
TallyDisplay.displayName = 'TallyDisplay';
|
|
280
284
|
export default TallyDisplay;
|
|
281
285
|
|
|
282
|
-
<
|
|
286
|
+
### <u>*reset.js*</u>
|
|
283
287
|
|
|
284
288
|
import React, { memo, useEffect } from 'react';
|
|
285
289
|
import { useObservableContext } from './context';
|
|
@@ -292,6 +296,6 @@ The context's `store` exposes **4** methods for interacting with the context's i
|
|
|
292
296
|
export default Reset;
|
|
293
297
|
|
|
294
298
|
|
|
295
|
-
|
|
299
|
+
# License
|
|
296
300
|
|
|
297
301
|
MIT
|
package/package.json
CHANGED