@webkrafters/react-observable-context 1.1.4 → 1.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 +324 -324
- package/dist/index.d.ts +9 -9
- package/dist/index.js +4 -4
- package/package.json +79 -79
package/README.md
CHANGED
|
@@ -1,324 +1,324 @@
|
|
|
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
|
-
<h4><u>Install</u></h4>
|
|
8
|
-
|
|
9
|
-
npm i -S @webkrafters/react-observable-context
|
|
10
|
-
|
|
11
|
-
npm install --save @webkrafters/react-observable-context
|
|
12
|
-
|
|
13
|
-
## API
|
|
14
|
-
|
|
15
|
-
The React-Observable-Context package exports only **2** modules namely: the **createContext** method and the **Provider** component.
|
|
16
|
-
|
|
17
|
-
`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`.
|
|
18
|
-
|
|
19
|
-
The `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>.
|
|
20
|
-
|
|
21
|
-
<i><u>Note:</u></i> the Provider `context` prop is not updateable. Once set, all further updates to this prop are not recorded.
|
|
22
|
-
|
|
23
|
-
The context's `store` exposes **4** methods for interacting with the context's internal state namely:
|
|
24
|
-
|
|
25
|
-
* **getState**: (selector?: (state: State) => any) => any
|
|
26
|
-
|
|
27
|
-
* **resetState**: VoidFunction // resets the state to the Provider initial `value` prop.
|
|
28
|
-
|
|
29
|
-
* **setState**: (changes: PartialState\<State\>) => void
|
|
30
|
-
|
|
31
|
-
* **subscribe**: (listener: (newValue: PartialState\<State\>, oldValue: PartialState\<State\>) => void) => ***UnsubscribeFunction***
|
|
32
|
-
|
|
33
|
-
### Prehooks
|
|
34
|
-
|
|
35
|
-
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.
|
|
36
|
-
|
|
37
|
-
* **resetState**: (state: {current: State, original: State}) => boolean
|
|
38
|
-
|
|
39
|
-
* **setState**: (newChanges: PartialState\<State\>) => boolean
|
|
40
|
-
|
|
41
|
-
***usecase***: 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 OR `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.
|
|
42
|
-
|
|
43
|
-
## Usage
|
|
44
|
-
|
|
45
|
-
<i><u>context.js</u></i>
|
|
46
|
-
|
|
47
|
-
import { createContext } from '@webkrafters/react-observable-context';
|
|
48
|
-
const ObservableContext = createContext();
|
|
49
|
-
export default ObservableContext;
|
|
50
|
-
|
|
51
|
-
<i><u>reset.js</u></i>
|
|
52
|
-
|
|
53
|
-
import React, { useContext } from 'react';
|
|
54
|
-
|
|
55
|
-
import ObservableContext from './context';
|
|
56
|
-
|
|
57
|
-
const Reset = () => {
|
|
58
|
-
|
|
59
|
-
const { resetState } = useContext( ObservableContext );
|
|
60
|
-
|
|
61
|
-
useEffect(() => console.log( 'Reset component rendered.....' ));
|
|
62
|
-
|
|
63
|
-
return ( <button onClick={ resetState }>reset context</button> );
|
|
64
|
-
};
|
|
65
|
-
Reset.displayName = 'Reset';
|
|
66
|
-
|
|
67
|
-
export default Reset;
|
|
68
|
-
|
|
69
|
-
<i><u>tally-display.js</u></i>
|
|
70
|
-
|
|
71
|
-
import React, { useContext, useEffect, useState } from 'react';
|
|
72
|
-
|
|
73
|
-
import ObservableContext from './context';
|
|
74
|
-
|
|
75
|
-
import Reset from './reset';
|
|
76
|
-
|
|
77
|
-
const TallyDisplay = () => {
|
|
78
|
-
|
|
79
|
-
const { getState, subscribe } = useContext( ObservableContext );
|
|
80
|
-
|
|
81
|
-
const [ , tripRender ] = useState( false );
|
|
82
|
-
|
|
83
|
-
useEffect(() => subscribe( newValue => {
|
|
84
|
-
[ 'color', 'price', 'type' ].some( k => k in newValue ) &&
|
|
85
|
-
tripRender( s => !s );
|
|
86
|
-
}), []);
|
|
87
|
-
|
|
88
|
-
useEffect(() => console.log( 'TallyDisplay component rendered.....' ));
|
|
89
|
-
|
|
90
|
-
return (
|
|
91
|
-
<div>
|
|
92
|
-
<table>
|
|
93
|
-
<tbody>
|
|
94
|
-
<tr><td><label>Type:</label></td><td>{ getState( s => s.type ) }</td></tr>
|
|
95
|
-
<tr><td><label>Color:</label></td><td>{ getState( s => s.color ) }</td></tr>
|
|
96
|
-
<tr><td><label>Price:</label></td><td>{ getState( s => s.price ).toFixed( 2 ) }</td></tr>
|
|
97
|
-
</tbody>
|
|
98
|
-
</table>
|
|
99
|
-
<div style={{ textAlign: 'right' }}>
|
|
100
|
-
<Reset />
|
|
101
|
-
</div>
|
|
102
|
-
</div>
|
|
103
|
-
);
|
|
104
|
-
};
|
|
105
|
-
TallyDisplay.displayName = 'TallyDisplay';
|
|
106
|
-
|
|
107
|
-
export default TallyDisplay;
|
|
108
|
-
|
|
109
|
-
<i><u>editor.js</u></i>
|
|
110
|
-
|
|
111
|
-
import React, { useCallback, useContext, useEffect, useRef } from 'react';
|
|
112
|
-
|
|
113
|
-
import ObservableContext from './context';
|
|
114
|
-
|
|
115
|
-
const Editor = () => {
|
|
116
|
-
|
|
117
|
-
const { setState } = useContext( ObservableContext );
|
|
118
|
-
|
|
119
|
-
const priceInputRef = useRef();
|
|
120
|
-
const colorInputRef = useRef();
|
|
121
|
-
const typeInputRef = useRef();
|
|
122
|
-
|
|
123
|
-
const updatePrice = useCallback(() => {
|
|
124
|
-
setState({ price: Number( priceInputRef.current.value ) });
|
|
125
|
-
}, []);
|
|
126
|
-
|
|
127
|
-
const updateColor = useCallback(() => {
|
|
128
|
-
setState({ color: colorInputRef.current.value });
|
|
129
|
-
}, []);
|
|
130
|
-
|
|
131
|
-
const updateType = useCallback(() => {
|
|
132
|
-
setState({ type: typeInputRef.current.value });
|
|
133
|
-
}, []);
|
|
134
|
-
|
|
135
|
-
useEffect(() => console.log( 'Editor component rendered.....' ));
|
|
136
|
-
|
|
137
|
-
return (
|
|
138
|
-
<fieldset style={{ margin: '10px 0' }}>
|
|
139
|
-
<legend>Editor</legend>
|
|
140
|
-
<div style={{ margin: '10px 0' }}>
|
|
141
|
-
<label>New Price: <input ref={ priceInputRef } /></label>
|
|
142
|
-
{ ' ' }
|
|
143
|
-
<button onClick={ updatePrice }>update price</button>
|
|
144
|
-
</div>
|
|
145
|
-
<div style={{ margin: '10px 0' }}>
|
|
146
|
-
<label>New Color: <input ref={ colorInputRef } /></label>
|
|
147
|
-
{ ' ' }
|
|
148
|
-
<button onClick={ updateColor }>update color</button>
|
|
149
|
-
</div>
|
|
150
|
-
<div style={{ margin: '10px 0' }}>
|
|
151
|
-
<label>New Type: <input ref={ typeInputRef } /></label>
|
|
152
|
-
{ ' ' }
|
|
153
|
-
<button onClick={ updateType }>update type</button>
|
|
154
|
-
</div>
|
|
155
|
-
</fieldset>
|
|
156
|
-
);
|
|
157
|
-
};
|
|
158
|
-
Editor.displayName = 'Editor';
|
|
159
|
-
|
|
160
|
-
export default Editor;
|
|
161
|
-
|
|
162
|
-
<i><u>product-description.js</u></i>
|
|
163
|
-
|
|
164
|
-
import React, { useContext, useEffect, useState } from 'react';
|
|
165
|
-
|
|
166
|
-
import ObservableContext from './context';
|
|
167
|
-
|
|
168
|
-
const ProductDescription = () => {
|
|
169
|
-
|
|
170
|
-
const store = useContext( ObservableContext );
|
|
171
|
-
|
|
172
|
-
const [ , tripRender ] = useState( false );
|
|
173
|
-
|
|
174
|
-
useEffect(() => store.subscribe( newValue => {
|
|
175
|
-
( 'color' in newValue || 'type' in newValue ) &&
|
|
176
|
-
tripRender( s => !s );
|
|
177
|
-
} ), []);
|
|
178
|
-
|
|
179
|
-
useEffect(() => console.log( 'ProductDescription component rendered.....' ));
|
|
180
|
-
|
|
181
|
-
const color = store.getState( s => s.color );
|
|
182
|
-
const type = store.getState( s => s.type );
|
|
183
|
-
|
|
184
|
-
return (
|
|
185
|
-
<div style={{ fontSize: 24 }}>
|
|
186
|
-
<strong>Description:</strong> { color } { type }
|
|
187
|
-
</div>
|
|
188
|
-
);
|
|
189
|
-
};
|
|
190
|
-
ProductDescription.displayName = 'ProductDescription';
|
|
191
|
-
|
|
192
|
-
export default ProductDescription;
|
|
193
|
-
|
|
194
|
-
<i><u>price-sticker.js</u></i>
|
|
195
|
-
|
|
196
|
-
import React, { useContext, useEffect, useState } from 'react';
|
|
197
|
-
|
|
198
|
-
import ObservableContext from './context';
|
|
199
|
-
|
|
200
|
-
const PriceSticker = () => {
|
|
201
|
-
|
|
202
|
-
const store = useContext( ObservableContext );
|
|
203
|
-
|
|
204
|
-
const [ price, setPrice ] = useState(() => store.getState( s => s.price ));
|
|
205
|
-
|
|
206
|
-
useEffect(() => store.subscribe( newValue => {
|
|
207
|
-
'price' in newValue && setPrice( newValue.price );
|
|
208
|
-
} ), []);
|
|
209
|
-
|
|
210
|
-
useEffect(() => console.log( 'PriceSticker component rendered.....' ));
|
|
211
|
-
|
|
212
|
-
return (
|
|
213
|
-
<div style={{ fontSize: 36, fontWeight: 800 }}>
|
|
214
|
-
${ price.toFixed( 2 ) }
|
|
215
|
-
</div>
|
|
216
|
-
);
|
|
217
|
-
};
|
|
218
|
-
PriceSticker.displayName = 'PriceSticker';
|
|
219
|
-
|
|
220
|
-
export default PriceSticker;
|
|
221
|
-
|
|
222
|
-
<i><u>product.js</u></i>
|
|
223
|
-
|
|
224
|
-
import React, { useCallback, useEffect, useState } from 'react';
|
|
225
|
-
import { Provider } from '@webkrafters/react-observable-context';
|
|
226
|
-
|
|
227
|
-
import ObservableContext from './context';
|
|
228
|
-
|
|
229
|
-
import Editor from './editor';
|
|
230
|
-
import PriceSticker from './price-sticker';
|
|
231
|
-
import ProductDescription from './product-description';
|
|
232
|
-
import TallyDisplay from './tally-display';
|
|
233
|
-
|
|
234
|
-
const Product = ({ prehooks = undefined, type }) => {
|
|
235
|
-
|
|
236
|
-
const [ state, setState ] = useState(() => ({
|
|
237
|
-
color: 'Burgundy',
|
|
238
|
-
price: 22.5,
|
|
239
|
-
type
|
|
240
|
-
}));
|
|
241
|
-
|
|
242
|
-
useEffect(() => {
|
|
243
|
-
setState({ type }); // use this to update only the changed state
|
|
244
|
-
// setState({ ...state, type }); // this will reset the context internal state
|
|
245
|
-
}, [ type ]);
|
|
246
|
-
|
|
247
|
-
const overridePricing = useCallback( e => setState({ price: Number( e.target.value ) }), [] );
|
|
248
|
-
|
|
249
|
-
return (
|
|
250
|
-
<div>
|
|
251
|
-
<div style={{ marginBottom: 10 }}>
|
|
252
|
-
<label>$ <input onKeyUp={ overridePricing } placeholder="override price here..."/></label>
|
|
253
|
-
</div>
|
|
254
|
-
<Provider
|
|
255
|
-
context={ ObservableContext }
|
|
256
|
-
prehooks={ prehooks }
|
|
257
|
-
value={ state }
|
|
258
|
-
>
|
|
259
|
-
<div style={{
|
|
260
|
-
borderBottom: '1px solid #333',
|
|
261
|
-
marginBottom: 10,
|
|
262
|
-
paddingBottom: 5
|
|
263
|
-
}}>
|
|
264
|
-
<Editor />
|
|
265
|
-
<TallyDisplay />
|
|
266
|
-
</div>
|
|
267
|
-
<ProductDescription />
|
|
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 = React.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
|
+
<h4><u>Install</u></h4>
|
|
8
|
+
|
|
9
|
+
npm i -S @webkrafters/react-observable-context
|
|
10
|
+
|
|
11
|
+
npm install --save @webkrafters/react-observable-context
|
|
12
|
+
|
|
13
|
+
## API
|
|
14
|
+
|
|
15
|
+
The React-Observable-Context package exports only **2** modules namely: the **createContext** method and the **Provider** component.
|
|
16
|
+
|
|
17
|
+
`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`.
|
|
18
|
+
|
|
19
|
+
The `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>.
|
|
20
|
+
|
|
21
|
+
<i><u>Note:</u></i> the Provider `context` prop is not updateable. Once set, all further updates to this prop are not recorded.
|
|
22
|
+
|
|
23
|
+
The context's `store` exposes **4** methods for interacting with the context's internal state namely:
|
|
24
|
+
|
|
25
|
+
* **getState**: (selector?: (state: State) => any) => any
|
|
26
|
+
|
|
27
|
+
* **resetState**: VoidFunction // resets the state to the Provider initial `value` prop.
|
|
28
|
+
|
|
29
|
+
* **setState**: (changes: PartialState\<State\>) => void
|
|
30
|
+
|
|
31
|
+
* **subscribe**: (listener: (newValue: PartialState\<State\>, oldValue: PartialState\<State\>) => void) => ***UnsubscribeFunction***
|
|
32
|
+
|
|
33
|
+
### Prehooks
|
|
34
|
+
|
|
35
|
+
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.
|
|
36
|
+
|
|
37
|
+
* **resetState**: (state: {current: State, original: State}) => boolean
|
|
38
|
+
|
|
39
|
+
* **setState**: (newChanges: PartialState\<State\>) => boolean
|
|
40
|
+
|
|
41
|
+
***usecase***: 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 OR `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.
|
|
42
|
+
|
|
43
|
+
## Usage
|
|
44
|
+
|
|
45
|
+
<i><u>context.js</u></i>
|
|
46
|
+
|
|
47
|
+
import { createContext } from '@webkrafters/react-observable-context';
|
|
48
|
+
const ObservableContext = createContext();
|
|
49
|
+
export default ObservableContext;
|
|
50
|
+
|
|
51
|
+
<i><u>reset.js</u></i>
|
|
52
|
+
|
|
53
|
+
import React, { useContext } from 'react';
|
|
54
|
+
|
|
55
|
+
import ObservableContext from './context';
|
|
56
|
+
|
|
57
|
+
const Reset = () => {
|
|
58
|
+
|
|
59
|
+
const { resetState } = useContext( ObservableContext );
|
|
60
|
+
|
|
61
|
+
useEffect(() => console.log( 'Reset component rendered.....' ));
|
|
62
|
+
|
|
63
|
+
return ( <button onClick={ resetState }>reset context</button> );
|
|
64
|
+
};
|
|
65
|
+
Reset.displayName = 'Reset';
|
|
66
|
+
|
|
67
|
+
export default Reset;
|
|
68
|
+
|
|
69
|
+
<i><u>tally-display.js</u></i>
|
|
70
|
+
|
|
71
|
+
import React, { useContext, useEffect, useState } from 'react';
|
|
72
|
+
|
|
73
|
+
import ObservableContext from './context';
|
|
74
|
+
|
|
75
|
+
import Reset from './reset';
|
|
76
|
+
|
|
77
|
+
const TallyDisplay = () => {
|
|
78
|
+
|
|
79
|
+
const { getState, subscribe } = useContext( ObservableContext );
|
|
80
|
+
|
|
81
|
+
const [ , tripRender ] = useState( false );
|
|
82
|
+
|
|
83
|
+
useEffect(() => subscribe( newValue => {
|
|
84
|
+
[ 'color', 'price', 'type' ].some( k => k in newValue ) &&
|
|
85
|
+
tripRender( s => !s );
|
|
86
|
+
}), []);
|
|
87
|
+
|
|
88
|
+
useEffect(() => console.log( 'TallyDisplay component rendered.....' ));
|
|
89
|
+
|
|
90
|
+
return (
|
|
91
|
+
<div>
|
|
92
|
+
<table>
|
|
93
|
+
<tbody>
|
|
94
|
+
<tr><td><label>Type:</label></td><td>{ getState( s => s.type ) }</td></tr>
|
|
95
|
+
<tr><td><label>Color:</label></td><td>{ getState( s => s.color ) }</td></tr>
|
|
96
|
+
<tr><td><label>Price:</label></td><td>{ getState( s => s.price ).toFixed( 2 ) }</td></tr>
|
|
97
|
+
</tbody>
|
|
98
|
+
</table>
|
|
99
|
+
<div style={{ textAlign: 'right' }}>
|
|
100
|
+
<Reset />
|
|
101
|
+
</div>
|
|
102
|
+
</div>
|
|
103
|
+
);
|
|
104
|
+
};
|
|
105
|
+
TallyDisplay.displayName = 'TallyDisplay';
|
|
106
|
+
|
|
107
|
+
export default TallyDisplay;
|
|
108
|
+
|
|
109
|
+
<i><u>editor.js</u></i>
|
|
110
|
+
|
|
111
|
+
import React, { useCallback, useContext, useEffect, useRef } from 'react';
|
|
112
|
+
|
|
113
|
+
import ObservableContext from './context';
|
|
114
|
+
|
|
115
|
+
const Editor = () => {
|
|
116
|
+
|
|
117
|
+
const { setState } = useContext( ObservableContext );
|
|
118
|
+
|
|
119
|
+
const priceInputRef = useRef();
|
|
120
|
+
const colorInputRef = useRef();
|
|
121
|
+
const typeInputRef = useRef();
|
|
122
|
+
|
|
123
|
+
const updatePrice = useCallback(() => {
|
|
124
|
+
setState({ price: Number( priceInputRef.current.value ) });
|
|
125
|
+
}, []);
|
|
126
|
+
|
|
127
|
+
const updateColor = useCallback(() => {
|
|
128
|
+
setState({ color: colorInputRef.current.value });
|
|
129
|
+
}, []);
|
|
130
|
+
|
|
131
|
+
const updateType = useCallback(() => {
|
|
132
|
+
setState({ type: typeInputRef.current.value });
|
|
133
|
+
}, []);
|
|
134
|
+
|
|
135
|
+
useEffect(() => console.log( 'Editor component rendered.....' ));
|
|
136
|
+
|
|
137
|
+
return (
|
|
138
|
+
<fieldset style={{ margin: '10px 0' }}>
|
|
139
|
+
<legend>Editor</legend>
|
|
140
|
+
<div style={{ margin: '10px 0' }}>
|
|
141
|
+
<label>New Price: <input ref={ priceInputRef } /></label>
|
|
142
|
+
{ ' ' }
|
|
143
|
+
<button onClick={ updatePrice }>update price</button>
|
|
144
|
+
</div>
|
|
145
|
+
<div style={{ margin: '10px 0' }}>
|
|
146
|
+
<label>New Color: <input ref={ colorInputRef } /></label>
|
|
147
|
+
{ ' ' }
|
|
148
|
+
<button onClick={ updateColor }>update color</button>
|
|
149
|
+
</div>
|
|
150
|
+
<div style={{ margin: '10px 0' }}>
|
|
151
|
+
<label>New Type: <input ref={ typeInputRef } /></label>
|
|
152
|
+
{ ' ' }
|
|
153
|
+
<button onClick={ updateType }>update type</button>
|
|
154
|
+
</div>
|
|
155
|
+
</fieldset>
|
|
156
|
+
);
|
|
157
|
+
};
|
|
158
|
+
Editor.displayName = 'Editor';
|
|
159
|
+
|
|
160
|
+
export default Editor;
|
|
161
|
+
|
|
162
|
+
<i><u>product-description.js</u></i>
|
|
163
|
+
|
|
164
|
+
import React, { useContext, useEffect, useState } from 'react';
|
|
165
|
+
|
|
166
|
+
import ObservableContext from './context';
|
|
167
|
+
|
|
168
|
+
const ProductDescription = () => {
|
|
169
|
+
|
|
170
|
+
const store = useContext( ObservableContext );
|
|
171
|
+
|
|
172
|
+
const [ , tripRender ] = useState( false );
|
|
173
|
+
|
|
174
|
+
useEffect(() => store.subscribe( newValue => {
|
|
175
|
+
( 'color' in newValue || 'type' in newValue ) &&
|
|
176
|
+
tripRender( s => !s );
|
|
177
|
+
} ), []);
|
|
178
|
+
|
|
179
|
+
useEffect(() => console.log( 'ProductDescription component rendered.....' ));
|
|
180
|
+
|
|
181
|
+
const color = store.getState( s => s.color );
|
|
182
|
+
const type = store.getState( s => s.type );
|
|
183
|
+
|
|
184
|
+
return (
|
|
185
|
+
<div style={{ fontSize: 24 }}>
|
|
186
|
+
<strong>Description:</strong> { color } { type }
|
|
187
|
+
</div>
|
|
188
|
+
);
|
|
189
|
+
};
|
|
190
|
+
ProductDescription.displayName = 'ProductDescription';
|
|
191
|
+
|
|
192
|
+
export default ProductDescription;
|
|
193
|
+
|
|
194
|
+
<i><u>price-sticker.js</u></i>
|
|
195
|
+
|
|
196
|
+
import React, { useContext, useEffect, useState } from 'react';
|
|
197
|
+
|
|
198
|
+
import ObservableContext from './context';
|
|
199
|
+
|
|
200
|
+
const PriceSticker = () => {
|
|
201
|
+
|
|
202
|
+
const store = useContext( ObservableContext );
|
|
203
|
+
|
|
204
|
+
const [ price, setPrice ] = useState(() => store.getState( s => s.price ));
|
|
205
|
+
|
|
206
|
+
useEffect(() => store.subscribe( newValue => {
|
|
207
|
+
'price' in newValue && setPrice( newValue.price );
|
|
208
|
+
} ), []);
|
|
209
|
+
|
|
210
|
+
useEffect(() => console.log( 'PriceSticker component rendered.....' ));
|
|
211
|
+
|
|
212
|
+
return (
|
|
213
|
+
<div style={{ fontSize: 36, fontWeight: 800 }}>
|
|
214
|
+
${ price.toFixed( 2 ) }
|
|
215
|
+
</div>
|
|
216
|
+
);
|
|
217
|
+
};
|
|
218
|
+
PriceSticker.displayName = 'PriceSticker';
|
|
219
|
+
|
|
220
|
+
export default PriceSticker;
|
|
221
|
+
|
|
222
|
+
<i><u>product.js</u></i>
|
|
223
|
+
|
|
224
|
+
import React, { useCallback, useEffect, useState } from 'react';
|
|
225
|
+
import { Provider } from '@webkrafters/react-observable-context';
|
|
226
|
+
|
|
227
|
+
import ObservableContext from './context';
|
|
228
|
+
|
|
229
|
+
import Editor from './editor';
|
|
230
|
+
import PriceSticker from './price-sticker';
|
|
231
|
+
import ProductDescription from './product-description';
|
|
232
|
+
import TallyDisplay from './tally-display';
|
|
233
|
+
|
|
234
|
+
const Product = ({ prehooks = undefined, type }) => {
|
|
235
|
+
|
|
236
|
+
const [ state, setState ] = useState(() => ({
|
|
237
|
+
color: 'Burgundy',
|
|
238
|
+
price: 22.5,
|
|
239
|
+
type
|
|
240
|
+
}));
|
|
241
|
+
|
|
242
|
+
useEffect(() => {
|
|
243
|
+
setState({ type }); // use this to update only the changed state
|
|
244
|
+
// setState({ ...state, type }); // this will reset the context internal state
|
|
245
|
+
}, [ type ]);
|
|
246
|
+
|
|
247
|
+
const overridePricing = useCallback( e => setState({ price: Number( e.target.value ) }), [] );
|
|
248
|
+
|
|
249
|
+
return (
|
|
250
|
+
<div>
|
|
251
|
+
<div style={{ marginBottom: 10 }}>
|
|
252
|
+
<label>$ <input onKeyUp={ overridePricing } placeholder="override price here..."/></label>
|
|
253
|
+
</div>
|
|
254
|
+
<Provider
|
|
255
|
+
context={ ObservableContext }
|
|
256
|
+
prehooks={ prehooks }
|
|
257
|
+
value={ state }
|
|
258
|
+
>
|
|
259
|
+
<div style={{
|
|
260
|
+
borderBottom: '1px solid #333',
|
|
261
|
+
marginBottom: 10,
|
|
262
|
+
paddingBottom: 5
|
|
263
|
+
}}>
|
|
264
|
+
<Editor />
|
|
265
|
+
<TallyDisplay />
|
|
266
|
+
</div>
|
|
267
|
+
<ProductDescription />
|
|
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 = React.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
|
package/dist/index.d.ts
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
export class UsageError extends Error {
|
|
2
2
|
}
|
|
3
|
-
export function createContext<T_1 extends State>():
|
|
3
|
+
export function createContext<T_1 extends State>(): React.Context<Store<T_2>>;
|
|
4
4
|
/**
|
|
5
5
|
* Note: `context` prop is not updateable. Furtther updates to this prop are ignored.
|
|
6
6
|
*
|
|
7
|
-
* @type {
|
|
8
|
-
* children?:
|
|
7
|
+
* @type {React.FC<{
|
|
8
|
+
* children?: React.ReactNode,
|
|
9
9
|
* context: ObservableContext<T>,
|
|
10
10
|
* prehooks?: Prehooks<T>
|
|
11
|
-
* value: T
|
|
11
|
+
* value: PartialState<T>
|
|
12
12
|
* }>}
|
|
13
13
|
* @template {State} T
|
|
14
14
|
*/
|
|
15
|
-
export const Provider:
|
|
16
|
-
children?:
|
|
17
|
-
context:
|
|
15
|
+
export const Provider: React.FC<{
|
|
16
|
+
children?: React.ReactNode;
|
|
17
|
+
context: React.Context<Store<T_1>>;
|
|
18
18
|
prehooks?: Prehooks<T>;
|
|
19
|
-
value: T
|
|
19
|
+
value: PartialState<T>;
|
|
20
20
|
}>;
|
|
21
|
-
export type ObservableContext<T_1 extends State> =
|
|
21
|
+
export type ObservableContext<T_1 extends State> = React.Context<Store<T>>;
|
|
22
22
|
export type OptionalTask<F = void> = F extends void ? () => never : F;
|
|
23
23
|
export type Listener<T_1 extends State> = (newValue: PartialState<T>, oldValue: PartialState<T>) => void;
|
|
24
24
|
export type State = {
|
package/dist/index.js
CHANGED
|
@@ -112,11 +112,11 @@ var usePrehooksRef = function usePrehooksRef(prehooks) {
|
|
|
112
112
|
/**
|
|
113
113
|
* Note: `context` prop is not updateable. Furtther updates to this prop are ignored.
|
|
114
114
|
*
|
|
115
|
-
* @type {
|
|
116
|
-
* children?:
|
|
115
|
+
* @type {React.FC<{
|
|
116
|
+
* children?: React.ReactNode,
|
|
117
117
|
* context: ObservableContext<T>,
|
|
118
118
|
* prehooks?: Prehooks<T>
|
|
119
|
-
* value: T
|
|
119
|
+
* value: PartialState<T>
|
|
120
120
|
* }>}
|
|
121
121
|
* @template {State} T
|
|
122
122
|
*/
|
|
@@ -205,7 +205,7 @@ exports.Provider = Provider;
|
|
|
205
205
|
Provider.displayName = 'ObservableContext.Provider';
|
|
206
206
|
|
|
207
207
|
/**
|
|
208
|
-
* @typedef {
|
|
208
|
+
* @typedef {React.Context<Store<T>>} ObservableContext
|
|
209
209
|
* @template {State} T
|
|
210
210
|
*/
|
|
211
211
|
|
package/package.json
CHANGED
|
@@ -1,79 +1,79 @@
|
|
|
1
|
-
{
|
|
2
|
-
"author": "Stephen Isienyi <stephen.isienyi@webkrafting.com>",
|
|
3
|
-
"bugs": {
|
|
4
|
-
"url": "https://github.com/steveswork/react-observable-context/issues"
|
|
5
|
-
},
|
|
6
|
-
"contributors": [
|
|
7
|
-
"steveswork <stephen.isienyi@webkrafting.com> (https://github.com/steveswork)"
|
|
8
|
-
],
|
|
9
|
-
"dependencies": {
|
|
10
|
-
"@types/react": "^18.0.17",
|
|
11
|
-
"lodash.clonedeep": "^4.5.0",
|
|
12
|
-
"lodash.isempty": "^4.4.0",
|
|
13
|
-
"react": "^18.2.0"
|
|
14
|
-
},
|
|
15
|
-
"description": "Observable react context - prevents an automatic total component tree tear-down and re-rendering during context updates.",
|
|
16
|
-
"devDependencies": {
|
|
17
|
-
"@babel/cli": "^7.17.0",
|
|
18
|
-
"@babel/core": "^7.12.10",
|
|
19
|
-
"@babel/node": "^7.12.10",
|
|
20
|
-
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.18.6",
|
|
21
|
-
"@babel/plugin-transform-runtime": "^7.17.0",
|
|
22
|
-
"@babel/preset-env": "^7.12.11",
|
|
23
|
-
"@babel/preset-react": "^7.18.6",
|
|
24
|
-
"@testing-library/jest-dom": "^5.16.5",
|
|
25
|
-
"@testing-library/react": "^13.4.0",
|
|
26
|
-
"@testing-library/user-event": "^14.4.3",
|
|
27
|
-
"@types/jest-cli": "^24.3.0",
|
|
28
|
-
"babel-jest": "^26.6.3",
|
|
29
|
-
"babel-loader": "^8.2.5",
|
|
30
|
-
"eslint": "^7.15.0",
|
|
31
|
-
"eslint-config-standard": "^16.0.2",
|
|
32
|
-
"eslint-plugin-import": "^2.22.1",
|
|
33
|
-
"eslint-plugin-jest": "^24.1.3",
|
|
34
|
-
"eslint-plugin-node": "^11.1.0",
|
|
35
|
-
"eslint-plugin-promise": "^4.2.1",
|
|
36
|
-
"eslint-plugin-react": "^7.31.11",
|
|
37
|
-
"eslint-plugin-standard": "^5.0.0",
|
|
38
|
-
"jest-cli": "^26.6.3",
|
|
39
|
-
"react-dom": "^18.2.0",
|
|
40
|
-
"react-performance-testing": "^2.0.0",
|
|
41
|
-
"typescript": "^4.8.2"
|
|
42
|
-
},
|
|
43
|
-
"files": [
|
|
44
|
-
"dist/index.js",
|
|
45
|
-
"dist/index.d.ts",
|
|
46
|
-
"index",
|
|
47
|
-
"package.json"
|
|
48
|
-
],
|
|
49
|
-
"homepage": "https://github.com/steveswork/react-observable-context#readme",
|
|
50
|
-
"jest": {
|
|
51
|
-
"transform": {
|
|
52
|
-
"\\.[jt]sx?$": "babel-jest"
|
|
53
|
-
}
|
|
54
|
-
},
|
|
55
|
-
"keywords": [
|
|
56
|
-
"context",
|
|
57
|
-
"react-hooks",
|
|
58
|
-
"react-context",
|
|
59
|
-
"react",
|
|
60
|
-
"useContext"
|
|
61
|
-
],
|
|
62
|
-
"license": "MIT",
|
|
63
|
-
"main": "index.js",
|
|
64
|
-
"name": "@webkrafters/react-observable-context",
|
|
65
|
-
"publishConfig": {
|
|
66
|
-
"access": "public"
|
|
67
|
-
},
|
|
68
|
-
"repository": {
|
|
69
|
-
"type": "git",
|
|
70
|
-
"url": "git+https://github.com/steveswork/react-observable-context.git"
|
|
71
|
-
},
|
|
72
|
-
"scripts": {
|
|
73
|
-
"build": "eslint --fix && rm -rf dist && babel src -d dist && npx -p typescript tsc",
|
|
74
|
-
"test": "eslint --fix && jest",
|
|
75
|
-
"test:watch": "eslint --fix && jest --watchAll"
|
|
76
|
-
},
|
|
77
|
-
"types": "dist/index.d.ts",
|
|
78
|
-
"version": "1.1.
|
|
79
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"author": "Stephen Isienyi <stephen.isienyi@webkrafting.com>",
|
|
3
|
+
"bugs": {
|
|
4
|
+
"url": "https://github.com/steveswork/react-observable-context/issues"
|
|
5
|
+
},
|
|
6
|
+
"contributors": [
|
|
7
|
+
"steveswork <stephen.isienyi@webkrafting.com> (https://github.com/steveswork)"
|
|
8
|
+
],
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"@types/react": "^18.0.17",
|
|
11
|
+
"lodash.clonedeep": "^4.5.0",
|
|
12
|
+
"lodash.isempty": "^4.4.0",
|
|
13
|
+
"react": "^18.2.0"
|
|
14
|
+
},
|
|
15
|
+
"description": "Observable react context - prevents an automatic total component tree tear-down and re-rendering during context updates.",
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@babel/cli": "^7.17.0",
|
|
18
|
+
"@babel/core": "^7.12.10",
|
|
19
|
+
"@babel/node": "^7.12.10",
|
|
20
|
+
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.18.6",
|
|
21
|
+
"@babel/plugin-transform-runtime": "^7.17.0",
|
|
22
|
+
"@babel/preset-env": "^7.12.11",
|
|
23
|
+
"@babel/preset-react": "^7.18.6",
|
|
24
|
+
"@testing-library/jest-dom": "^5.16.5",
|
|
25
|
+
"@testing-library/react": "^13.4.0",
|
|
26
|
+
"@testing-library/user-event": "^14.4.3",
|
|
27
|
+
"@types/jest-cli": "^24.3.0",
|
|
28
|
+
"babel-jest": "^26.6.3",
|
|
29
|
+
"babel-loader": "^8.2.5",
|
|
30
|
+
"eslint": "^7.15.0",
|
|
31
|
+
"eslint-config-standard": "^16.0.2",
|
|
32
|
+
"eslint-plugin-import": "^2.22.1",
|
|
33
|
+
"eslint-plugin-jest": "^24.1.3",
|
|
34
|
+
"eslint-plugin-node": "^11.1.0",
|
|
35
|
+
"eslint-plugin-promise": "^4.2.1",
|
|
36
|
+
"eslint-plugin-react": "^7.31.11",
|
|
37
|
+
"eslint-plugin-standard": "^5.0.0",
|
|
38
|
+
"jest-cli": "^26.6.3",
|
|
39
|
+
"react-dom": "^18.2.0",
|
|
40
|
+
"react-performance-testing": "^2.0.0",
|
|
41
|
+
"typescript": "^4.8.2"
|
|
42
|
+
},
|
|
43
|
+
"files": [
|
|
44
|
+
"dist/index.js",
|
|
45
|
+
"dist/index.d.ts",
|
|
46
|
+
"index",
|
|
47
|
+
"package.json"
|
|
48
|
+
],
|
|
49
|
+
"homepage": "https://github.com/steveswork/react-observable-context#readme",
|
|
50
|
+
"jest": {
|
|
51
|
+
"transform": {
|
|
52
|
+
"\\.[jt]sx?$": "babel-jest"
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
"keywords": [
|
|
56
|
+
"context",
|
|
57
|
+
"react-hooks",
|
|
58
|
+
"react-context",
|
|
59
|
+
"react",
|
|
60
|
+
"useContext"
|
|
61
|
+
],
|
|
62
|
+
"license": "MIT",
|
|
63
|
+
"main": "index.js",
|
|
64
|
+
"name": "@webkrafters/react-observable-context",
|
|
65
|
+
"publishConfig": {
|
|
66
|
+
"access": "public"
|
|
67
|
+
},
|
|
68
|
+
"repository": {
|
|
69
|
+
"type": "git",
|
|
70
|
+
"url": "git+https://github.com/steveswork/react-observable-context.git"
|
|
71
|
+
},
|
|
72
|
+
"scripts": {
|
|
73
|
+
"build": "eslint --fix && rm -rf dist && babel src -d dist && npx -p typescript tsc",
|
|
74
|
+
"test": "eslint --fix && jest",
|
|
75
|
+
"test:watch": "eslint --fix && jest --watchAll"
|
|
76
|
+
},
|
|
77
|
+
"types": "dist/index.d.ts",
|
|
78
|
+
"version": "1.1.5"
|
|
79
|
+
}
|