@webkrafters/react-observable-context 4.0.0-rc.2 → 4.0.0-rc.4

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 +21 -19
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -252,7 +252,6 @@ The React-Observable-Context module contains **4** exports namely:
252
252
  <i><b><u>context.js</u></b></i>
253
253
  ```
254
254
  import { createContext } from '@webkrafters/react-observable-context';
255
-
256
255
  export default createContext();
257
256
  ```
258
257
 
@@ -323,19 +322,11 @@ export default Ui;
323
322
 
324
323
  <i id="provider-usage"><b><u>provider.js</u></b></i>
325
324
  ```
326
- import React, { useCallback, useMemo, useState } from 'react';
325
+ import React, { useEffect, useState } from 'react';
327
326
  import ObservableContext from './context';
328
327
  import Ui from './ui';
329
328
 
330
- const initialState = { a: { b: { c: 25, x: { y: { z: [ 2022 ] } } } } };
331
-
332
- const createStorageStub = data => ({
333
- clone( data ) { return <your clone function>( data ) },
334
- data,
335
- getItem( key ) { return this.data },
336
- removeItem( key ) {},
337
- setItem( key, data ) {}
338
- });
329
+ const DEFAULT_C = 36;
339
330
 
340
331
  const updateHooks = {
341
332
  resetState: ( ...args ) => {
@@ -348,24 +339,35 @@ const updateHooks = {
348
339
  }
349
340
  };
350
341
 
351
- const Provider = ({ c = initialState.c }) => {
352
-
353
- const storage = useMemo(() => createStorageStub({ ...initialsState, c }), []);
342
+ const storageStub = {
343
+ clone( data ) { return <your clone function>( data ) },
344
+ data: null,
345
+ getItem( key ) { return this.data },
346
+ removeItem( key ) { this.data = null },
347
+ setItem( key, data ) { this.data = data }
348
+ };
349
+
350
+ const Provider = ({ c = DEFAULT_C }) => {
354
351
 
355
- const [ state, setState ] = useState(() => storage.getItem());
352
+ const [ state, setState ] = useState(() => ({
353
+ a: { b: { c, x: { y: { z: [ 2022 ] } } } });
354
+ }));
356
355
 
357
356
  useEffect(() => {
358
- setState({ c }); // use this (similar to `store.setState`) to update only the changed slice of the context internal state.
359
- // Do not do this: `setState({ ...state, c });` // it will override the context internal state.
357
+ // similar to `store.setState`, use the following to update
358
+ // only the changed slice of the context internal state.
359
+ setState({ a: { b: { c } } });
360
+ // Do not do the following: it will override the context internal state.
361
+ // setState({ ...state, a: { ...state.a, b: { ...state.a.b, c } } });
360
362
  }, [ c ]);
361
363
 
362
364
  return (
363
365
  <ObservableContext.Provider
364
366
  prehooks={ updateHooks }
365
- storage={ storage }
367
+ storage={ storageStub }
366
368
  value={ state }
367
369
  >
368
- <Client />
370
+ <Ui />
369
371
  </ObservableContext.Provider>
370
372
  );
371
373
  };
package/package.json CHANGED
@@ -129,5 +129,5 @@
129
129
  "test:watch": "eslint --fix && jest --updateSnapshot --watchAll"
130
130
  },
131
131
  "types": "dist/main/index.d.ts",
132
- "version": "4.0.0-rc.2"
132
+ "version": "4.0.0-rc.4"
133
133
  }