@webkrafters/react-observable-context 2.1.1 → 2.1.3
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 +287 -267
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,267 +1,287 @@
|
|
|
1
|
-
# React-Observable-Context
|
|
2
|
-
|
|
3
|
-
A context bearing an observable consumer 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.
|
|
4
|
-
|
|
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
|
-
|
|
7
|
-
_**Recommendation:**_ Wrap all components calling this package's ***useContext*** function in **React::memo**. This will protect such
|
|
8
|
-
|
|
9
|
-
<
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
npm
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
* **
|
|
22
|
-
|
|
23
|
-
* **
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
The
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
* **
|
|
34
|
-
|
|
35
|
-
* **
|
|
36
|
-
|
|
37
|
-
* **
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
* **
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
<
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
const
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
<
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
1
|
+
# React-Observable-Context
|
|
2
|
+
|
|
3
|
+
A context bearing an observable consumer 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.
|
|
4
|
+
|
|
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
|
+
|
|
7
|
+
_**Recommendation:**_ Wrap all components calling this package's ***useContext*** function in **React::memo**. This will protect such components and their descendants from unrelated cascading render operations.
|
|
8
|
+
|
|
9
|
+
<hr />
|
|
10
|
+
|
|
11
|
+
<h4><u>Install</u></h4>
|
|
12
|
+
|
|
13
|
+
npm i -S @webkrafters/react-observable-context
|
|
14
|
+
|
|
15
|
+
npm install --save @webkrafters/react-observable-context
|
|
16
|
+
|
|
17
|
+
## API
|
|
18
|
+
|
|
19
|
+
The React-Observable-Context package exports **3** modules namely: the **createContext** method, the **useContext** hook and the **Provider** component.
|
|
20
|
+
|
|
21
|
+
* **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
|
+
|
|
23
|
+
* **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.
|
|
24
|
+
|
|
25
|
+
* **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
|
+
|
|
27
|
+
***<u>Note:</u>*** the Provider `context` prop is not updateable. Once set, all further updates to this prop are not recorded.
|
|
28
|
+
|
|
29
|
+
### The Store
|
|
30
|
+
|
|
31
|
+
The context's `store` exposes **4** methods for interacting with the context's internal state namely:
|
|
32
|
+
|
|
33
|
+
* **getState**: (selector?: (state: State) => any) => any
|
|
34
|
+
|
|
35
|
+
* **resetState**: VoidFunction // resets the state to the Provider initial `value` prop.
|
|
36
|
+
|
|
37
|
+
* **setState**: (changes: PartialState\<State\>) => void // sets only new/changed top level properties.
|
|
38
|
+
|
|
39
|
+
* **subscribe**: (listener: (newValue: PartialState\<State\>, oldValue: PartialState\<State\>) => void) => ***UnsubscribeFunction***
|
|
40
|
+
|
|
41
|
+
### Prehooks
|
|
42
|
+
|
|
43
|
+
The context's store update operation adheres to **2** user supplied prehooks when present. Otherwise, the update operation proceeds normally to completion. They are named **resetState** and **setState** after the store update methods which utilize them.
|
|
44
|
+
|
|
45
|
+
* **resetState**: (state: {current: State, original: State}) => boolean
|
|
46
|
+
|
|
47
|
+
* **setState**: (newChanges: PartialState\<State\>) => boolean
|
|
48
|
+
|
|
49
|
+
***<u>Use case:</u>*** prehooks provide a central place for sanitizing, modifying, transforming, validating etc. all related incoming state updates. The prehook returns a **boolean** value (`true` to continue AND `false` to abort the update operation). The prehook may mutate (i.e. sanitize, transform, transpose) its argument values to accurately reflect the intended update value.
|
|
50
|
+
|
|
51
|
+
## Usage
|
|
52
|
+
|
|
53
|
+
<i><u>context.js</u></i>
|
|
54
|
+
|
|
55
|
+
import React from 'react';
|
|
56
|
+
|
|
57
|
+
import {
|
|
58
|
+
createContext,
|
|
59
|
+
Provider,
|
|
60
|
+
useContext
|
|
61
|
+
} from '@webkrafters/react-observable-context';
|
|
62
|
+
|
|
63
|
+
export const ObservableContext = createContext();
|
|
64
|
+
|
|
65
|
+
export const ObservableContextProvider = ({ children, prehooks, value }) => (
|
|
66
|
+
<Provider
|
|
67
|
+
context={ ObservableContext }
|
|
68
|
+
prehooks={ prehooks }
|
|
69
|
+
value={ value }
|
|
70
|
+
>
|
|
71
|
+
{ children }
|
|
72
|
+
</Provider>
|
|
73
|
+
);
|
|
74
|
+
ObservableContextProvider.displayName = 'ObservableContextProvider';
|
|
75
|
+
|
|
76
|
+
export const useObservableContext = watchedKeys => useContext( ObservableContext, watchedKeys );
|
|
77
|
+
|
|
78
|
+
export default ObservableContext;
|
|
79
|
+
|
|
80
|
+
<i><u>index.js</i></b>
|
|
81
|
+
|
|
82
|
+
import React from 'react';
|
|
83
|
+
import ReactDOM from 'react-dom';
|
|
84
|
+
import App from './app';
|
|
85
|
+
ReactDOM.render(<App />, document.getElementById('root'));
|
|
86
|
+
|
|
87
|
+
<i><u>app.js</u></i>
|
|
88
|
+
|
|
89
|
+
import React, { useCallback, useState } from 'react';
|
|
90
|
+
import Product from './product';
|
|
91
|
+
const productUpdateHooks = {
|
|
92
|
+
resetState: ( ...args ) => {
|
|
93
|
+
console.log( 'resetting state with >>>> ', JSON.stringify( args ) );
|
|
94
|
+
return true;
|
|
95
|
+
},
|
|
96
|
+
setState: ( ...args ) => {
|
|
97
|
+
console.log( 'setting state with >>>> ', JSON.stringify( args ) );
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
const App = () => {
|
|
102
|
+
const [ productType, setProductType ] = useState( 'Calculator' );
|
|
103
|
+
const updateType = useCallback( e => setProductType( e.target.value ), [] );
|
|
104
|
+
return (
|
|
105
|
+
<div className="App">
|
|
106
|
+
<h1>Demo</h1>
|
|
107
|
+
<h2>A contrived product app.</h2>
|
|
108
|
+
<div style={{ marginBottom: 10 }}>
|
|
109
|
+
<label>Type: <input onKeyUp={ updateType } placeholder="override product type here..." /></label>
|
|
110
|
+
</div>
|
|
111
|
+
<Product
|
|
112
|
+
type={ productType }
|
|
113
|
+
updateHooks={ productUpdateHooks }
|
|
114
|
+
/>
|
|
115
|
+
</div>
|
|
116
|
+
);
|
|
117
|
+
};
|
|
118
|
+
App.displayName = 'App';
|
|
119
|
+
export default App;
|
|
120
|
+
|
|
121
|
+
<i><u>product.js</u></i>
|
|
122
|
+
|
|
123
|
+
import React, { useCallback, useEffect, useState } from 'react';
|
|
124
|
+
import { ObservableContextProvider } from './context';
|
|
125
|
+
import Editor from './editor';
|
|
126
|
+
import PriceSticker from './price-sticker';
|
|
127
|
+
import ProductDescription from './product-description';
|
|
128
|
+
import TallyDisplay from './tally-display';
|
|
129
|
+
const Product = ({ type, updateHooks }) => {
|
|
130
|
+
const [ state, setState ] = useState(() => ({
|
|
131
|
+
color: 'Burgundy',
|
|
132
|
+
price: 22.5,
|
|
133
|
+
type
|
|
134
|
+
}));
|
|
135
|
+
useEffect(() => {
|
|
136
|
+
setState({ type }); // use this to update only the changed state value
|
|
137
|
+
// setState({ ...state, type }); // this will override the context internal state for these values
|
|
138
|
+
}, [ type ]);
|
|
139
|
+
const overridePricing = useCallback( e => setState({ price: Number( e.target.value ) }), [] );
|
|
140
|
+
return (
|
|
141
|
+
<div>
|
|
142
|
+
<div style={{ marginBottom: 10 }}>
|
|
143
|
+
<label>$ <input onKeyUp={ overridePricing } placeholder="override price here..." /></label>
|
|
144
|
+
</div>
|
|
145
|
+
<ObservableContextProvider
|
|
146
|
+
prehooks={ updateHooks }
|
|
147
|
+
value={ state }
|
|
148
|
+
>
|
|
149
|
+
<div style={{
|
|
150
|
+
borderBottom: '1px solid #333',
|
|
151
|
+
marginBottom: 10,
|
|
152
|
+
paddingBottom: 5
|
|
153
|
+
}}>
|
|
154
|
+
<Editor />
|
|
155
|
+
<TallyDisplay />
|
|
156
|
+
</div>
|
|
157
|
+
<ProductDescription />
|
|
158
|
+
<PriceSticker />
|
|
159
|
+
</ObservableContextProvider>
|
|
160
|
+
</div>
|
|
161
|
+
);
|
|
162
|
+
};
|
|
163
|
+
Product.displayName = 'Product';
|
|
164
|
+
export default Product;
|
|
165
|
+
|
|
166
|
+
<i><u>editor.js</u></i>
|
|
167
|
+
|
|
168
|
+
import React, { memo, useCallback, useEffect, useRef } from 'react';
|
|
169
|
+
import { useObservableContext } from './context';
|
|
170
|
+
const Editor = memo(() => {
|
|
171
|
+
const { setState } = useObservableContext();
|
|
172
|
+
const priceInputRef = useRef();
|
|
173
|
+
const colorInputRef = useRef();
|
|
174
|
+
const typeInputRef = useRef();
|
|
175
|
+
const updatePrice = useCallback(() => {
|
|
176
|
+
setState({ price: Number( priceInputRef.current.value ) });
|
|
177
|
+
}, []);
|
|
178
|
+
const updateColor = useCallback(() => {
|
|
179
|
+
setState({ color: colorInputRef.current.value });
|
|
180
|
+
}, []);
|
|
181
|
+
const updateType = useCallback(() => {
|
|
182
|
+
setState({ type: typeInputRef.current.value });
|
|
183
|
+
}, []);
|
|
184
|
+
useEffect(() => console.log( 'Editor component rendered.....' ));
|
|
185
|
+
return (
|
|
186
|
+
<fieldset style={{ margin: '10px 0' }}>
|
|
187
|
+
<legend>Editor</legend>
|
|
188
|
+
<div style={{ margin: '10px 0' }}>
|
|
189
|
+
<label>New Price: <input ref={ priceInputRef } /></label>
|
|
190
|
+
{ ' ' }
|
|
191
|
+
<button onClick={ updatePrice }>update price</button>
|
|
192
|
+
</div>
|
|
193
|
+
<div style={{ margin: '10px 0' }}>
|
|
194
|
+
<label>New Color: <input ref={ colorInputRef } /></label>
|
|
195
|
+
{ ' ' }
|
|
196
|
+
<button onClick={ updateColor }>update color</button>
|
|
197
|
+
</div>
|
|
198
|
+
<div style={{ margin: '10px 0' }}>
|
|
199
|
+
<label>New Type: <input ref={ typeInputRef } /></label>
|
|
200
|
+
{ ' ' }
|
|
201
|
+
<button onClick={ updateType }>update type</button>
|
|
202
|
+
</div>
|
|
203
|
+
</fieldset>
|
|
204
|
+
);
|
|
205
|
+
});
|
|
206
|
+
Editor.displayName = 'Editor';
|
|
207
|
+
export default Editor;
|
|
208
|
+
|
|
209
|
+
<i><u>price-sticker.js</u></i>
|
|
210
|
+
|
|
211
|
+
import React, { memo, useEffect } from 'react';
|
|
212
|
+
import { useObservableContext } from './context';
|
|
213
|
+
const CONTEXT_KEYS = [ 'price' ];
|
|
214
|
+
const PriceSticker = memo(() => {
|
|
215
|
+
const { getState } = useObservableContext( CONTEXT_KEYS );
|
|
216
|
+
useEffect(() => console.log( 'PriceSticker component rendered.....' ));
|
|
217
|
+
return (
|
|
218
|
+
<div style={{ fontSize: 36, fontWeight: 800 }}>
|
|
219
|
+
${ getState( s => s.price ).toFixed( 2 ) }
|
|
220
|
+
</div>
|
|
221
|
+
);
|
|
222
|
+
});
|
|
223
|
+
PriceSticker.displayName = 'PriceSticker';
|
|
224
|
+
export default PriceSticker;
|
|
225
|
+
|
|
226
|
+
<i><u>product-description.js</u></i>
|
|
227
|
+
|
|
228
|
+
import React, { memo, useEffect } from 'react';
|
|
229
|
+
import { useObservableContext } from './context';
|
|
230
|
+
const CONTEXT_KEYS = [ 'color', 'type' ];
|
|
231
|
+
const ProductDescription = memo(() => {
|
|
232
|
+
const store = useObservableContext( CONTEXT_KEYS );
|
|
233
|
+
useEffect(() => console.log( 'ProductDescription component rendered.....' ));
|
|
234
|
+
const color = store.getState( s => s.color );
|
|
235
|
+
const type = store.getState( s => s.type );
|
|
236
|
+
return (
|
|
237
|
+
<div style={{ fontSize: 24 }}>
|
|
238
|
+
<strong>Description:</strong> { color } { type }
|
|
239
|
+
</div>
|
|
240
|
+
);
|
|
241
|
+
});
|
|
242
|
+
ProductDescription.displayName = 'ProductDescription';
|
|
243
|
+
export default ProductDescription;
|
|
244
|
+
|
|
245
|
+
<i><u>tally-display.js</u></i>
|
|
246
|
+
|
|
247
|
+
import React, { memo, useEffect } from 'react';
|
|
248
|
+
import { useObservableContext } from './context';
|
|
249
|
+
import Reset from './reset';
|
|
250
|
+
const CONTEXT_KEYS = [ 'color', 'price', 'type' ];
|
|
251
|
+
const TallyDisplay = memo(() => {
|
|
252
|
+
const { getState } = useObservableContext( CONTEXT_KEYS );
|
|
253
|
+
useEffect(() => console.log( 'TallyDisplay component rendered.....' ));
|
|
254
|
+
return (
|
|
255
|
+
<div>
|
|
256
|
+
<table>
|
|
257
|
+
<tbody>
|
|
258
|
+
<tr><td><label>Type:</label></td><td>{ getState( s => s.type ) }</td></tr>
|
|
259
|
+
<tr><td><label>Color:</label></td><td>{ getState( s => s.color ) }</td></tr>
|
|
260
|
+
<tr><td><label>Price:</label></td><td>{ getState( s => s.price ).toFixed( 2 ) }</td></tr>
|
|
261
|
+
</tbody>
|
|
262
|
+
</table>
|
|
263
|
+
<div style={{ textAlign: 'right' }}>
|
|
264
|
+
<Reset />
|
|
265
|
+
</div>
|
|
266
|
+
</div>
|
|
267
|
+
);
|
|
268
|
+
});
|
|
269
|
+
TallyDisplay.displayName = 'TallyDisplay';
|
|
270
|
+
export default TallyDisplay;
|
|
271
|
+
|
|
272
|
+
<i><u>reset.js</u></i>
|
|
273
|
+
|
|
274
|
+
import React, { memo, useEffect } from 'react';
|
|
275
|
+
import { useObservableContext } from './context';
|
|
276
|
+
const Reset = memo(() => {
|
|
277
|
+
const { resetState } = useObservableContext();
|
|
278
|
+
useEffect(() => console.log( 'Reset component rendered.....' ));
|
|
279
|
+
return ( <button onClick={ resetState }>reset context</button> );
|
|
280
|
+
});
|
|
281
|
+
Reset.displayName = 'Reset';
|
|
282
|
+
export default Reset;
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
## License
|
|
286
|
+
|
|
287
|
+
MIT
|
package/package.json
CHANGED