@webkrafters/react-observable-context 2.0.1 → 2.1.1
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 +267 -324
- package/dist/index.d.ts +11 -8
- package/dist/index.js +86 -13
- package/package.json +5 -3
package/README.md
CHANGED
|
@@ -1,324 +1,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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
npm
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
* **
|
|
20
|
-
|
|
21
|
-
* **
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
The
|
|
28
|
-
|
|
29
|
-
|
|
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
|
-
import ObservableContext from './context';
|
|
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
|
-
import
|
|
230
|
-
import
|
|
231
|
-
|
|
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
|
-
|
|
268
|
-
<PriceSticker />
|
|
269
|
-
</Provider>
|
|
270
|
-
</div>
|
|
271
|
-
);
|
|
272
|
-
};
|
|
273
|
-
Product.displayName = 'Product';
|
|
274
|
-
|
|
275
|
-
export default Product;
|
|
276
|
-
|
|
277
|
-
<i><u>app.js</u></i>
|
|
278
|
-
|
|
279
|
-
import React, { useCallback, useMemo, useState } from 'react';
|
|
280
|
-
|
|
281
|
-
import Product from './product';
|
|
282
|
-
|
|
283
|
-
const App = () => {
|
|
284
|
-
|
|
285
|
-
const [ productType, setProductType ] = useState( 'Calculator' );
|
|
286
|
-
|
|
287
|
-
const updateType = useCallback( e => setProductType( e.target.value ), [] );
|
|
288
|
-
|
|
289
|
-
const prehooks = useMemo(() => ({
|
|
290
|
-
resetState: ( ...args ) => {
|
|
291
|
-
console.log( 'resetting state with >>>> ', JSON.stringify( args ) );
|
|
292
|
-
return true;
|
|
293
|
-
},
|
|
294
|
-
setState: ( ...args ) => {
|
|
295
|
-
console.log( 'setting state with >>>> ', JSON.stringify( args ) );
|
|
296
|
-
return true;
|
|
297
|
-
}
|
|
298
|
-
}), []);
|
|
299
|
-
|
|
300
|
-
return (
|
|
301
|
-
<div className="App">
|
|
302
|
-
<h1>Demo</h1>
|
|
303
|
-
<h2>A contrived product app.</h2>
|
|
304
|
-
<div style={{ marginBottom: 10 }}>
|
|
305
|
-
<label>Type: <input onKeyUp={ updateType } placeholder="override product type here..." /></label>
|
|
306
|
-
</div>
|
|
307
|
-
<Product prehooks={ prehooks } type={ productType } />
|
|
308
|
-
</div>
|
|
309
|
-
);
|
|
310
|
-
};
|
|
311
|
-
App.displayName = 'App';
|
|
312
|
-
|
|
313
|
-
export default App;
|
|
314
|
-
|
|
315
|
-
<i><u>index.js</i></b>
|
|
316
|
-
|
|
317
|
-
import React from 'react';
|
|
318
|
-
import ReactDOM from 'react-dom';
|
|
319
|
-
import App from './app';
|
|
320
|
-
ReactDOM.render(<App />, document.getElementById('root'));
|
|
321
|
-
|
|
322
|
-
## License
|
|
323
|
-
|
|
324
|
-
MIT
|
|
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 component and its descendants from unrelated cascading render operations.
|
|
8
|
+
|
|
9
|
+
<h4><u>Install</u></h4>
|
|
10
|
+
|
|
11
|
+
npm i -S @webkrafters/react-observable-context
|
|
12
|
+
|
|
13
|
+
npm install --save @webkrafters/react-observable-context
|
|
14
|
+
|
|
15
|
+
## API
|
|
16
|
+
|
|
17
|
+
The React-Observable-Context package exports **3** modules namely: the **createContext** method, the **useContext** hook and the **Provider** component.
|
|
18
|
+
|
|
19
|
+
* **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`.
|
|
20
|
+
|
|
21
|
+
* **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.
|
|
22
|
+
|
|
23
|
+
* **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>.
|
|
24
|
+
|
|
25
|
+
***<u>Note:</u>*** the Provider `context` prop is not updateable. Once set, all further updates to this prop are not recorded.
|
|
26
|
+
|
|
27
|
+
### The Store
|
|
28
|
+
|
|
29
|
+
The context's `store` exposes **4** methods for interacting with the context's internal state namely:
|
|
30
|
+
|
|
31
|
+
* **getState**: (selector?: (state: State) => any) => any
|
|
32
|
+
|
|
33
|
+
* **resetState**: VoidFunction // resets the state to the Provider initial `value` prop.
|
|
34
|
+
|
|
35
|
+
* **setState**: (changes: PartialState\<State\>) => void // sets only new/changed top level properties.
|
|
36
|
+
|
|
37
|
+
* **subscribe**: (listener: (newValue: PartialState\<State\>, oldValue: PartialState\<State\>) => void) => ***UnsubscribeFunction***
|
|
38
|
+
|
|
39
|
+
### Prehooks
|
|
40
|
+
|
|
41
|
+
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.
|
|
42
|
+
|
|
43
|
+
* **resetState**: (state: {current: State, original: State}) => boolean
|
|
44
|
+
|
|
45
|
+
* **setState**: (newChanges: PartialState\<State\>) => boolean
|
|
46
|
+
|
|
47
|
+
***<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.
|
|
48
|
+
|
|
49
|
+
## Usage
|
|
50
|
+
|
|
51
|
+
<i><u>context.js</u></i>
|
|
52
|
+
|
|
53
|
+
import { createContext } from '@webkrafters/react-observable-context';
|
|
54
|
+
const ObservableContext = createContext();
|
|
55
|
+
export default ObservableContext;
|
|
56
|
+
|
|
57
|
+
<i><u>reset.js</u></i>
|
|
58
|
+
|
|
59
|
+
import React, { memo, useEffect } from 'react';
|
|
60
|
+
import { useContext } from '@webkrafters/react-observable-context';
|
|
61
|
+
import ObservableContext from './context';
|
|
62
|
+
const Reset = memo(() => {
|
|
63
|
+
const { resetState } = useContext( ObservableContext );
|
|
64
|
+
useEffect(() => console.log( 'Reset component rendered.....' ));
|
|
65
|
+
return ( <button onClick={ resetState }>reset context</button> );
|
|
66
|
+
});
|
|
67
|
+
Reset.displayName = 'Reset';
|
|
68
|
+
export default Reset;
|
|
69
|
+
|
|
70
|
+
<i><u>tally-display.js</u></i>
|
|
71
|
+
|
|
72
|
+
import React, { memo, useEffect } from 'react';
|
|
73
|
+
import { useContext } from '@webkrafters/react-observable-context';
|
|
74
|
+
import ObservableContext from './context';
|
|
75
|
+
import Reset from './reset';
|
|
76
|
+
const CONTEXT_KEYS = [ 'color', 'price', 'type' ];
|
|
77
|
+
const TallyDisplay = memo(() => {
|
|
78
|
+
const { getState } = useContext( ObservableContext, CONTEXT_KEYS );
|
|
79
|
+
useEffect(() => console.log( 'TallyDisplay component rendered.....' ));
|
|
80
|
+
return (
|
|
81
|
+
<div>
|
|
82
|
+
<table>
|
|
83
|
+
<tbody>
|
|
84
|
+
<tr><td><label>Type:</label></td><td>{ getState( s => s.type ) }</td></tr>
|
|
85
|
+
<tr><td><label>Color:</label></td><td>{ getState( s => s.color ) }</td></tr>
|
|
86
|
+
<tr><td><label>Price:</label></td><td>{ getState( s => s.price ).toFixed( 2 ) }</td></tr>
|
|
87
|
+
</tbody>
|
|
88
|
+
</table>
|
|
89
|
+
<div style={{ textAlign: 'right' }}>
|
|
90
|
+
<Reset />
|
|
91
|
+
</div>
|
|
92
|
+
</div>
|
|
93
|
+
);
|
|
94
|
+
});
|
|
95
|
+
TallyDisplay.displayName = 'TallyDisplay';
|
|
96
|
+
export default TallyDisplay;
|
|
97
|
+
|
|
98
|
+
<i><u>editor.js</u></i>
|
|
99
|
+
|
|
100
|
+
import React, { memo, useCallback, useEffect, useRef } from 'react';
|
|
101
|
+
import { useContext } from '@webkrafters/react-observable-context';
|
|
102
|
+
import ObservableContext from './context';
|
|
103
|
+
const Editor = memo(() => {
|
|
104
|
+
const { setState } = useContext( ObservableContext );
|
|
105
|
+
const priceInputRef = useRef();
|
|
106
|
+
const colorInputRef = useRef();
|
|
107
|
+
const typeInputRef = useRef();
|
|
108
|
+
const updatePrice = useCallback(() => {
|
|
109
|
+
setState({ price: Number( priceInputRef.current.value ) });
|
|
110
|
+
}, []);
|
|
111
|
+
const updateColor = useCallback(() => {
|
|
112
|
+
setState({ color: colorInputRef.current.value });
|
|
113
|
+
}, []);
|
|
114
|
+
const updateType = useCallback(() => {
|
|
115
|
+
setState({ type: typeInputRef.current.value });
|
|
116
|
+
}, []);
|
|
117
|
+
useEffect(() => console.log( 'Editor component rendered.....' ));
|
|
118
|
+
return (
|
|
119
|
+
<fieldset style={{ margin: '10px 0' }}>
|
|
120
|
+
<legend>Editor</legend>
|
|
121
|
+
<div style={{ margin: '10px 0' }}>
|
|
122
|
+
<label>New Price: <input ref={ priceInputRef } /></label>
|
|
123
|
+
{ ' ' }
|
|
124
|
+
<button onClick={ updatePrice }>update price</button>
|
|
125
|
+
</div>
|
|
126
|
+
<div style={{ margin: '10px 0' }}>
|
|
127
|
+
<label>New Color: <input ref={ colorInputRef } /></label>
|
|
128
|
+
{ ' ' }
|
|
129
|
+
<button onClick={ updateColor }>update color</button>
|
|
130
|
+
</div>
|
|
131
|
+
<div style={{ margin: '10px 0' }}>
|
|
132
|
+
<label>New Type: <input ref={ typeInputRef } /></label>
|
|
133
|
+
{ ' ' }
|
|
134
|
+
<button onClick={ updateType }>update type</button>
|
|
135
|
+
</div>
|
|
136
|
+
</fieldset>
|
|
137
|
+
);
|
|
138
|
+
});
|
|
139
|
+
Editor.displayName = 'Editor';
|
|
140
|
+
export default Editor;
|
|
141
|
+
|
|
142
|
+
<i><u>product-description.js</u></i>
|
|
143
|
+
|
|
144
|
+
import React, { memo, useEffect } from 'react';
|
|
145
|
+
import { useContext } from '@webkrafters/react-observable-context';
|
|
146
|
+
import ObservableContext from './context';
|
|
147
|
+
const CONTEXT_KEYS = [ 'color', 'type' ];
|
|
148
|
+
const ProductDescription = memo(() => {
|
|
149
|
+
const store = useContext( ObservableContext, CONTEXT_KEYS );
|
|
150
|
+
useEffect(() => console.log( 'ProductDescription component rendered.....' ));
|
|
151
|
+
const color = store.getState( s => s.color );
|
|
152
|
+
const type = store.getState( s => s.type );
|
|
153
|
+
return (
|
|
154
|
+
<div style={{ fontSize: 24 }}>
|
|
155
|
+
<strong>Description:</strong> { color } { type }
|
|
156
|
+
</div>
|
|
157
|
+
);
|
|
158
|
+
});
|
|
159
|
+
ProductDescription.displayName = 'ProductDescription';
|
|
160
|
+
export default ProductDescription;
|
|
161
|
+
|
|
162
|
+
<i><u>price-sticker.js</u></i>
|
|
163
|
+
|
|
164
|
+
import React, { memo, useEffect } from 'react';
|
|
165
|
+
import { useContext } from '@webkrafters/react-observable-context';
|
|
166
|
+
import ObservableContext from './context';
|
|
167
|
+
const CONTEXT_KEYS = [ 'price' ];
|
|
168
|
+
const PriceSticker = memo(() => {
|
|
169
|
+
const { getState } = useContext( ObservableContext, CONTEXT_KEYS );
|
|
170
|
+
useEffect(() => console.log( 'PriceSticker component rendered.....' ));
|
|
171
|
+
return (
|
|
172
|
+
<div style={{ fontSize: 36, fontWeight: 800 }}>
|
|
173
|
+
${ getState( s => s.price ).toFixed( 2 ) }
|
|
174
|
+
</div>
|
|
175
|
+
);
|
|
176
|
+
});
|
|
177
|
+
PriceSticker.displayName = 'PriceSticker';
|
|
178
|
+
export default PriceSticker;
|
|
179
|
+
|
|
180
|
+
<i><u>product.js</u></i>
|
|
181
|
+
|
|
182
|
+
import React, { useCallback, useEffect, useState } from 'react';
|
|
183
|
+
import { Provider } from '@webkrafters/react-observable-context';
|
|
184
|
+
import ObservableContext from './context';
|
|
185
|
+
import Editor from './editor';
|
|
186
|
+
import PriceSticker from './price-sticker';
|
|
187
|
+
import ProductDescription from './product-description';
|
|
188
|
+
import TallyDisplay from './tally-display';
|
|
189
|
+
const Product = ({ prehooks = undefined, type }) => {
|
|
190
|
+
const [ state, setState ] = useState(() => ({
|
|
191
|
+
color: 'Burgundy',
|
|
192
|
+
price: 22.5,
|
|
193
|
+
type
|
|
194
|
+
}));
|
|
195
|
+
useEffect(() => {
|
|
196
|
+
setState({ type }); // use this to update only the changed state
|
|
197
|
+
// setState({ ...state, type }); // this will reset the context internal state
|
|
198
|
+
}, [ type ]);
|
|
199
|
+
const overridePricing = useCallback( e => setState({ price: Number( e.target.value ) }), [] );
|
|
200
|
+
return (
|
|
201
|
+
<div>
|
|
202
|
+
<div style={{ marginBottom: 10 }}>
|
|
203
|
+
<label>$ <input onKeyUp={ overridePricing } placeholder="override price here..."/></label>
|
|
204
|
+
</div>
|
|
205
|
+
<Provider
|
|
206
|
+
context={ ObservableContext }
|
|
207
|
+
prehooks={ prehooks }
|
|
208
|
+
value={ state }
|
|
209
|
+
>
|
|
210
|
+
<div style={{
|
|
211
|
+
borderBottom: '1px solid #333',
|
|
212
|
+
marginBottom: 10,
|
|
213
|
+
paddingBottom: 5
|
|
214
|
+
}}>
|
|
215
|
+
<Editor />
|
|
216
|
+
<TallyDisplay />
|
|
217
|
+
</div>
|
|
218
|
+
<ProductDescription />
|
|
219
|
+
<PriceSticker />
|
|
220
|
+
</Provider>
|
|
221
|
+
</div>
|
|
222
|
+
);
|
|
223
|
+
};
|
|
224
|
+
Product.displayName = 'Product';
|
|
225
|
+
export default Product;
|
|
226
|
+
|
|
227
|
+
<i><u>app.js</u></i>
|
|
228
|
+
|
|
229
|
+
import React, { useCallback, useMemo, useState } from 'react';
|
|
230
|
+
import Product from './product';
|
|
231
|
+
const prehooks = {
|
|
232
|
+
resetState: ( ...args ) => {
|
|
233
|
+
console.log( 'resetting state with >>>> ', JSON.stringify( args ) );
|
|
234
|
+
return true;
|
|
235
|
+
},
|
|
236
|
+
setState: ( ...args ) => {
|
|
237
|
+
console.log( 'setting state with >>>> ', JSON.stringify( args ) );
|
|
238
|
+
return true;
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
const App = () => {
|
|
242
|
+
const [ productType, setProductType ] = useState( 'Calculator' );
|
|
243
|
+
const updateType = useCallback( e => setProductType( e.target.value ), [] );
|
|
244
|
+
return (
|
|
245
|
+
<div className="App">
|
|
246
|
+
<h1>Demo</h1>
|
|
247
|
+
<h2>A contrived product app.</h2>
|
|
248
|
+
<div style={{ marginBottom: 10 }}>
|
|
249
|
+
<label>Type: <input onKeyUp={ updateType } placeholder="override product type here..." /></label>
|
|
250
|
+
</div>
|
|
251
|
+
<Product prehooks={ prehooks } type={ productType } />
|
|
252
|
+
</div>
|
|
253
|
+
);
|
|
254
|
+
};
|
|
255
|
+
App.displayName = 'App';
|
|
256
|
+
export default App;
|
|
257
|
+
|
|
258
|
+
<i><u>index.js</i></b>
|
|
259
|
+
|
|
260
|
+
import React from 'react';
|
|
261
|
+
import ReactDOM from 'react-dom';
|
|
262
|
+
import App from './app';
|
|
263
|
+
ReactDOM.render(<App />, document.getElementById('root'));
|
|
264
|
+
|
|
265
|
+
## License
|
|
266
|
+
|
|
267
|
+
MIT
|
package/dist/index.d.ts
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
export class UsageError extends Error {
|
|
2
2
|
}
|
|
3
|
-
export function createContext<T_1 extends State>():
|
|
4
|
-
export function useContext<T_1 extends State>(context:
|
|
3
|
+
export function createContext<T_1 extends State>(): import("react").Context<Store<T_1>>;
|
|
4
|
+
export function useContext<T_1 extends State>(context: import("react").Context<Store<T_1>>, watchedKeys?: (string | keyof T_1)[]): Store<T_1>;
|
|
5
5
|
/**
|
|
6
6
|
* Note: `context` prop is not updateable. Furtther updates to this prop are ignored.
|
|
7
7
|
*
|
|
8
|
-
* @type {
|
|
9
|
-
* children?:
|
|
8
|
+
* @type {FC<{
|
|
9
|
+
* children?: ReactNode,
|
|
10
10
|
* context: ObservableContext<T>,
|
|
11
11
|
* prehooks?: Prehooks<T>
|
|
12
12
|
* value: PartialState<T>
|
|
13
13
|
* }>}
|
|
14
14
|
* @template {State} T
|
|
15
15
|
*/
|
|
16
|
-
export const Provider:
|
|
17
|
-
children?:
|
|
18
|
-
context:
|
|
16
|
+
export const Provider: FC<{
|
|
17
|
+
children?: ReactNode;
|
|
18
|
+
context: ObservableContext<T>;
|
|
19
19
|
prehooks?: Prehooks<T>;
|
|
20
20
|
value: PartialState<T>;
|
|
21
21
|
}>;
|
|
22
|
-
export type ObservableContext<T_1 extends State> =
|
|
22
|
+
export type ObservableContext<T_1 extends State> = Context<Store<T>>;
|
|
23
23
|
export type OptionalTask<F = void> = F extends void ? () => never : F;
|
|
24
24
|
export type Listener<T_1 extends State> = (newValue: PartialState<T>, oldValue: PartialState<T>) => void;
|
|
25
25
|
export type State = {
|
|
@@ -43,4 +43,7 @@ export type Store<T_1 extends State> = {
|
|
|
43
43
|
subscribe: (listener: Listener<T>) => Unsubscribe;
|
|
44
44
|
};
|
|
45
45
|
export type Unsubscribe = VoidFunction;
|
|
46
|
+
export type ReactNode = import("react").ReactNode;
|
|
47
|
+
export type FC<P = {}> = import("react").FC<P>;
|
|
48
|
+
export type Context<T_1> = import("react").Context<T>;
|
|
46
49
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
4
3
|
Object.defineProperty(exports, "__esModule", {
|
|
5
4
|
value: true
|
|
6
5
|
});
|
|
@@ -9,9 +8,12 @@ var _react = _interopRequireWildcard(require("react"));
|
|
|
9
8
|
var _lodash = _interopRequireDefault(require("lodash.clonedeep"));
|
|
10
9
|
var _lodash2 = _interopRequireDefault(require("lodash.has"));
|
|
11
10
|
var _lodash3 = _interopRequireDefault(require("lodash.isempty"));
|
|
11
|
+
var _lodash4 = _interopRequireDefault(require("lodash.isequal"));
|
|
12
|
+
var _lodash5 = _interopRequireDefault(require("lodash.omit"));
|
|
12
13
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
13
14
|
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
14
15
|
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
16
|
+
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
15
17
|
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
16
18
|
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
17
19
|
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
@@ -144,24 +146,82 @@ var usePrehooksRef = function usePrehooksRef(prehooks) {
|
|
|
144
146
|
return prehooksRef;
|
|
145
147
|
};
|
|
146
148
|
|
|
149
|
+
/** @type {FC<{child: ReactNode}>} */
|
|
150
|
+
var ChildMemo = function () {
|
|
151
|
+
var useNodeMemo = function useNodeMemo(node) {
|
|
152
|
+
var nodeRef = (0, _react.useRef)(node);
|
|
153
|
+
if (!(0, _lodash4["default"])((0, _lodash5["default"])(nodeRef.current, '_owner'), (0, _lodash5["default"])(node, '_owner'))) {
|
|
154
|
+
nodeRef.current = node;
|
|
155
|
+
}
|
|
156
|
+
return nodeRef.current;
|
|
157
|
+
};
|
|
158
|
+
var ChildMemo = /*#__PURE__*/(0, _react.memo)(function (_ref) {
|
|
159
|
+
var child = _ref.child;
|
|
160
|
+
return child;
|
|
161
|
+
});
|
|
162
|
+
ChildMemo.displayName = 'ObservableContext.Provider.Internal.Guardian.ChildMemo';
|
|
163
|
+
var Guardian = function Guardian(_ref2) {
|
|
164
|
+
var child = _ref2.child;
|
|
165
|
+
return /*#__PURE__*/_react["default"].createElement(ChildMemo, {
|
|
166
|
+
child: useNodeMemo(child)
|
|
167
|
+
});
|
|
168
|
+
};
|
|
169
|
+
Guardian.displayName = 'ObservableContext.Provider.Internal.Guardian';
|
|
170
|
+
return Guardian;
|
|
171
|
+
}();
|
|
172
|
+
|
|
173
|
+
/** @type {(children: ReactNode) => ReactNode} */
|
|
174
|
+
var memoizeImmediateChildTree = function memoizeImmediateChildTree(children) {
|
|
175
|
+
return _react.Children.map(children, function (child) {
|
|
176
|
+
var _child$props;
|
|
177
|
+
if (_typeof(child.type) === 'object' && 'compare' in child.type) {
|
|
178
|
+
return child;
|
|
179
|
+
}
|
|
180
|
+
if ((_child$props = child.props) !== null && _child$props !== void 0 && _child$props.children) {
|
|
181
|
+
child = /*#__PURE__*/(0, _react.cloneElement)(child, (0, _lodash5["default"])(child.props, 'children'), memoizeImmediateChildTree(child.props.children));
|
|
182
|
+
}
|
|
183
|
+
return /*#__PURE__*/_react["default"].createElement(ChildMemo, {
|
|
184
|
+
child: child
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* @type {FC<{
|
|
191
|
+
* children?: ReactNode,
|
|
192
|
+
* context: ObservableContext<T>,
|
|
193
|
+
* value: Store<T>
|
|
194
|
+
* }>}
|
|
195
|
+
* @template {State} T
|
|
196
|
+
*/
|
|
197
|
+
var ProviderInternal = function ProviderInternal(_ref3) {
|
|
198
|
+
var children = _ref3.children,
|
|
199
|
+
StoreContext = _ref3.context,
|
|
200
|
+
value = _ref3.value;
|
|
201
|
+
return /*#__PURE__*/_react["default"].createElement(StoreContext.Provider, {
|
|
202
|
+
value: value
|
|
203
|
+
}, memoizeImmediateChildTree(children));
|
|
204
|
+
};
|
|
205
|
+
ProviderInternal.displayName = 'ObservableContext.Provider.Internal';
|
|
206
|
+
|
|
147
207
|
/**
|
|
148
208
|
* Note: `context` prop is not updateable. Furtther updates to this prop are ignored.
|
|
149
209
|
*
|
|
150
|
-
* @type {
|
|
151
|
-
* children?:
|
|
210
|
+
* @type {FC<{
|
|
211
|
+
* children?: ReactNode,
|
|
152
212
|
* context: ObservableContext<T>,
|
|
153
213
|
* prehooks?: Prehooks<T>
|
|
154
214
|
* value: PartialState<T>
|
|
155
215
|
* }>}
|
|
156
216
|
* @template {State} T
|
|
157
217
|
*/
|
|
158
|
-
var Provider = function Provider(
|
|
159
|
-
var
|
|
160
|
-
children =
|
|
161
|
-
context =
|
|
162
|
-
|
|
163
|
-
prehooks =
|
|
164
|
-
value =
|
|
218
|
+
var Provider = function Provider(_ref4) {
|
|
219
|
+
var _ref4$children = _ref4.children,
|
|
220
|
+
children = _ref4$children === void 0 ? null : _ref4$children,
|
|
221
|
+
context = _ref4.context,
|
|
222
|
+
_ref4$prehooks = _ref4.prehooks,
|
|
223
|
+
prehooks = _ref4$prehooks === void 0 ? defaultPrehooks : _ref4$prehooks,
|
|
224
|
+
value = _ref4.value;
|
|
165
225
|
var prehooksRef = usePrehooksRef(prehooks);
|
|
166
226
|
var initialState = (0, _react.useRef)(value);
|
|
167
227
|
|
|
@@ -232,7 +292,8 @@ var Provider = function Provider(_ref) {
|
|
|
232
292
|
}),
|
|
233
293
|
_useState10 = _slicedToArray(_useState9, 1),
|
|
234
294
|
store = _useState10[0];
|
|
235
|
-
return /*#__PURE__*/_react["default"].createElement(
|
|
295
|
+
return /*#__PURE__*/_react["default"].createElement(ProviderInternal, {
|
|
296
|
+
context: StoreContext,
|
|
236
297
|
value: store
|
|
237
298
|
}, children);
|
|
238
299
|
};
|
|
@@ -240,7 +301,7 @@ exports.Provider = Provider;
|
|
|
240
301
|
Provider.displayName = 'ObservableContext.Provider';
|
|
241
302
|
|
|
242
303
|
/**
|
|
243
|
-
* @typedef {
|
|
304
|
+
* @typedef {Context<Store<T>>} ObservableContext
|
|
244
305
|
* @template {State} T
|
|
245
306
|
*/
|
|
246
307
|
|
|
@@ -284,4 +345,16 @@ Provider.displayName = 'ObservableContext.Provider';
|
|
|
284
345
|
* @template {State} T
|
|
285
346
|
*/
|
|
286
347
|
|
|
287
|
-
/** @typedef {VoidFunction} Unsubscribe */
|
|
348
|
+
/** @typedef {VoidFunction} Unsubscribe */
|
|
349
|
+
|
|
350
|
+
/** @typedef {import("react").ReactNode} ReactNode */
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* @typedef {import("react").FC<P>} FC
|
|
354
|
+
* @template [P={}]
|
|
355
|
+
*/
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* @typedef {import("react").Context<T>} Context
|
|
359
|
+
* @template T
|
|
360
|
+
*/
|
package/package.json
CHANGED
|
@@ -11,6 +11,8 @@
|
|
|
11
11
|
"lodash.clonedeep": "^4.5.0",
|
|
12
12
|
"lodash.has": "^4.5.2",
|
|
13
13
|
"lodash.isempty": "^4.4.0",
|
|
14
|
+
"lodash.isequal": "^4.5.0",
|
|
15
|
+
"lodash.omit": "^4.5.0",
|
|
14
16
|
"react": "^18.2.0"
|
|
15
17
|
},
|
|
16
18
|
"description": "Observable react context - prevents an automatic total component tree tear-down and re-rendering during context updates.",
|
|
@@ -72,9 +74,9 @@
|
|
|
72
74
|
},
|
|
73
75
|
"scripts": {
|
|
74
76
|
"build": "eslint --fix && rm -rf dist && babel src -d dist && npx -p typescript tsc",
|
|
75
|
-
"test": "eslint --fix && jest",
|
|
76
|
-
"test:watch": "eslint --fix && jest --watchAll"
|
|
77
|
+
"test": "eslint --fix && jest --updateSnapshot",
|
|
78
|
+
"test:watch": "eslint --fix && jest --updateSnapshot --watchAll"
|
|
77
79
|
},
|
|
78
80
|
"types": "dist/index.d.ts",
|
|
79
|
-
"version": "2.
|
|
81
|
+
"version": "2.1.1"
|
|
80
82
|
}
|