@webkrafters/react-observable-context 1.1.0 → 1.1.2

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.
Files changed (2) hide show
  1. package/README.md +56 -17
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -14,7 +14,7 @@ The React-Observable-Context package exports only **2** modules namely: the **cr
14
14
 
15
15
  `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`.
16
16
 
17
- 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` props <i>(discussed in the prehooks section below)</i>.
17
+ 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>.
18
18
 
19
19
  <i><u>Note:</u></i> the Provider `context` prop is not updateable. Once set, all further updates to this prop are ignored.
20
20
 
@@ -46,33 +46,58 @@ The context's store update operation adheres to **2** user supplied prehooks whe
46
46
  const ObservableContext = createContext();
47
47
  export default ObservableContext;
48
48
 
49
+ <i><u>reset.js</u></i>
50
+
51
+ import React, { useContext } from 'react';
52
+
53
+ import ObservableContext from './context';
54
+
55
+ const Reset = () => {
56
+
57
+ const { resetState } = useContext( ObservableContext );
58
+
59
+ useEffect(() => console.log( 'Reset component rendered.....' ));
60
+
61
+ return ( <button onClick={ resetState }>reset context</button> );
62
+ };
63
+ Reset.displayName = 'Reset';
64
+
65
+ export default Reset;
66
+
49
67
  <i><u>tally-display.js</u></i>
50
68
 
51
69
  import React, { useContext, useEffect, useState } from 'react';
52
70
 
53
71
  import ObservableContext from './context';
72
+
73
+ import Reset from './reset';
54
74
 
55
75
  const TallyDisplay = () => {
56
76
 
57
77
  const { getState, subscribe } = useContext( ObservableContext );
58
78
 
59
- const [ , setUpdateTs ] = useState();
79
+ const [ , tripRender ] = useState( false );
60
80
 
61
81
  useEffect(() => subscribe( newValue => {
62
82
  [ 'color', 'price', 'type' ].some( k => k in newValue ) &&
63
- setUpdateTs( Date.now() );
83
+ tripRender( s => !s );
64
84
  }), []);
65
85
 
66
86
  useEffect(() => console.log( 'TallyDisplay component rendered.....' ));
67
87
 
68
88
  return (
69
- <table>
70
- <tbody>
71
- <tr><td><label>Type:</label></td><td>{ getState( s => s.type ) }</td></tr>
72
- <tr><td><label>Color:</label></td><td>{ getState( s => s.color ) }</td></tr>
73
- <tr><td><label>Price:</label></td><td>{ getState( s => s.price ).toFixed( 2 ) }</td></tr>
74
- </tbody>
75
- </table>
89
+ <div>
90
+ <table>
91
+ <tbody>
92
+ <tr><td><label>Type:</label></td><td>{ getState( s => s.type ) }</td></tr>
93
+ <tr><td><label>Color:</label></td><td>{ getState( s => s.color ) }</td></tr>
94
+ <tr><td><label>Price:</label></td><td>{ getState( s => s.price ).toFixed( 2 ) }</td></tr>
95
+ </tbody>
96
+ </table>
97
+ <div style={{ textAlign: 'right' }}>
98
+ <Reset />
99
+ </div>
100
+ </div>
76
101
  );
77
102
  };
78
103
  TallyDisplay.displayName = 'TallyDisplay';
@@ -142,11 +167,11 @@ The context's store update operation adheres to **2** user supplied prehooks whe
142
167
 
143
168
  const store = useContext( ObservableContext );
144
169
 
145
- const [ , setUpdateTs ] = useState();
170
+ const [ , tripRender ] = useState( false );
146
171
 
147
172
  useEffect(() => store.subscribe( newValue => {
148
173
  ( 'color' in newValue || 'type' in newValue ) &&
149
- setUpdateTs( Date.now() );
174
+ tripRender( s => !s );
150
175
  } ), []);
151
176
 
152
177
  useEffect(() => console.log( 'ProductDescription component rendered.....' ));
@@ -204,7 +229,7 @@ The context's store update operation adheres to **2** user supplied prehooks whe
204
229
  import ProductDescription from './product-description';
205
230
  import TallyDisplay from './tally-display';
206
231
 
207
- const Product = ({ type }) => {
232
+ const Product = ({ prehooks = undefined, type }) => {
208
233
 
209
234
  const [ state, setState ] = useState(() => ({
210
235
  color: 'Burgundy',
@@ -224,7 +249,11 @@ The context's store update operation adheres to **2** user supplied prehooks whe
224
249
  <div style={{ marginBottom: 10 }}>
225
250
  <label>$ <input onKeyUp={ overridePricing } placeholder="override price here..."/></label>
226
251
  </div>
227
- <Provider context={ ObservableContext } value={ state }>
252
+ <Provider
253
+ context={ ObservableContext }
254
+ prehooks={ prehooks }
255
+ value={ state }
256
+ >
228
257
  <div style={{
229
258
  borderBottom: '1px solid #333',
230
259
  marginBottom: 10,
@@ -245,7 +274,7 @@ The context's store update operation adheres to **2** user supplied prehooks whe
245
274
 
246
275
  <i><u>app.js</u></i>
247
276
 
248
- import React, { useCallback, useState } from 'react';
277
+ import React, { useCallback, useMemo, useState } from 'react';
249
278
 
250
279
  import Product from './product';
251
280
 
@@ -254,6 +283,17 @@ The context's store update operation adheres to **2** user supplied prehooks whe
254
283
  const [ productType, setProductType ] = useState( 'Calculator' );
255
284
 
256
285
  const updateType = useCallback( e => setProductType( e.target.value ), [] );
286
+
287
+ const prehooks = React.useMemo(() => ({
288
+ resetState: ( ...args ) => {
289
+ console.log( 'resetting state with >>>> ', JSON.stringify( args ) );
290
+ return true;
291
+ },
292
+ setState: ( ...args ) => {
293
+ console.log( 'setting state with >>>> ', JSON.stringify( args ) );
294
+ return true;
295
+ }
296
+ }), []);
257
297
 
258
298
  return (
259
299
  <div className="App">
@@ -262,10 +302,9 @@ The context's store update operation adheres to **2** user supplied prehooks whe
262
302
  <div style={{ marginBottom: 10 }}>
263
303
  <label>Type: <input onKeyUp={ updateType } placeholder="override product type here..." /></label>
264
304
  </div>
265
- <Product type={ productType } />
305
+ <Product prehooks={ prehooks } type={ productType } />
266
306
  </div>
267
307
  );
268
-
269
308
  };
270
309
  App.displayName = 'App';
271
310
 
package/package.json CHANGED
@@ -75,5 +75,5 @@
75
75
  "test:watch": "eslint --fix && jest --watchAll"
76
76
  },
77
77
  "types": "dist/index.d.ts",
78
- "version": "1.1.0"
78
+ "version": "1.1.2"
79
79
  }