@webkrafters/react-observable-context 2.1.4-rc.0 → 2.1.5
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 +7 -3
- package/dist/index.js +7 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,11 +4,13 @@ A context bearing an observable consumer store. State changes within the store's
|
|
|
4
4
|
|
|
5
5
|
**React::memo** *(and PureComponents)* remain the go-to solution for the repeated automatic re-renderings of entire component trees resulting from ***component*** state changes.
|
|
6
6
|
|
|
7
|
-
_**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.
|
|
7
|
+
_**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.
|
|
8
|
+
|
|
9
|
+
***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.
|
|
8
10
|
|
|
9
11
|
<hr />
|
|
10
12
|
|
|
11
|
-
<
|
|
13
|
+
<h3><u>Install</u></h3>
|
|
12
14
|
|
|
13
15
|
npm i -S @webkrafters/react-observable-context
|
|
14
16
|
|
|
@@ -16,7 +18,7 @@ npm install --save @webkrafters/react-observable-context
|
|
|
16
18
|
|
|
17
19
|
## API
|
|
18
20
|
|
|
19
|
-
The React-Observable-Context package exports **
|
|
21
|
+
The React-Observable-Context package exports **4** modules namely: the **createContext** method, the **useContext** hook, the **Provider** component and the **UsageError** class.
|
|
20
22
|
|
|
21
23
|
* **createContext** is a zero-parameter funtion returning a store-bearing context. Pass the context to the React::useContext() parameter to obtain the context's `store`.
|
|
22
24
|
|
|
@@ -24,6 +26,8 @@ The React-Observable-Context package exports **3** modules namely: the **createC
|
|
|
24
26
|
|
|
25
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>.
|
|
26
28
|
|
|
29
|
+
* **UsageError** class is the Error type reported for attempts to access this context's store outside of its Provider component tree.
|
|
30
|
+
|
|
27
31
|
***<u>Note:</u>*** the Provider `context` prop is not updateable. Once set, all further updates to this prop are not recorded.
|
|
28
32
|
|
|
29
33
|
### The Store
|
package/dist/index.js
CHANGED
|
@@ -113,6 +113,10 @@ var useContext = function useContext(context) {
|
|
|
113
113
|
exports.useContext = useContext;
|
|
114
114
|
var defaultPrehooks = Object.freeze({});
|
|
115
115
|
var _setState = function () {
|
|
116
|
+
var initDiff = function initDiff(propKey, changed, replaced) {
|
|
117
|
+
changed[propKey] = {};
|
|
118
|
+
replaced[propKey] = {};
|
|
119
|
+
};
|
|
116
120
|
var setAtomic = function setAtomic(state, newState, changed, replaced, stateKey) {
|
|
117
121
|
if ((0, _lodash4["default"])(state[stateKey], newState[stateKey])) {
|
|
118
122
|
return;
|
|
@@ -130,18 +134,14 @@ var _setState = function () {
|
|
|
130
134
|
changed[stateKey] = newState[stateKey];
|
|
131
135
|
};
|
|
132
136
|
var setArray = function setArray(state, newState, changed, replaced, rootKey) {
|
|
133
|
-
|
|
134
|
-
replaced[rootKey] = {};
|
|
137
|
+
initDiff(rootKey, changed, replaced);
|
|
135
138
|
for (var i = 0, len = newState[rootKey].length; i < len; i++) {
|
|
136
139
|
setAtomic(state[rootKey], newState[rootKey], changed[rootKey], replaced[rootKey], i);
|
|
137
140
|
}
|
|
138
141
|
};
|
|
139
142
|
var setPlainObject = function setPlainObject(state, newState, changed, replaced, rootKey) {
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
for (var k in newState[rootKey]) {
|
|
143
|
-
setAtomic(state[rootKey], newState[rootKey], changed[rootKey], replaced[rootKey], k);
|
|
144
|
-
}
|
|
143
|
+
initDiff(rootKey, changed, replaced);
|
|
144
|
+
set(state[rootKey], newState[rootKey], changed[rootKey], replaced[rootKey]);
|
|
145
145
|
};
|
|
146
146
|
var set = function set(state, newState) {
|
|
147
147
|
var changed = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
package/package.json
CHANGED