@webkrafters/react-observable-context 3.0.0-rc.0 → 3.0.0

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 (3) hide show
  1. package/README.md +284 -284
  2. package/dist/index.js +9 -33
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -1,284 +1,284 @@
1
- # React-Observable-Context
2
-
3
- A context bearing an observable consumer [store](#store).\
4
- Only re-renders subscribing components on context state change.\
5
- Subscribing component decides which context state properties' change to trigger its update.
6
-
7
- **Name:** React-Observable-Context
8
-
9
- **Usage:** Please see [Usage](#usage) section
10
-
11
- **Install:**\
12
- npm i -S @webkrafters/react-observable-context\
13
- npm install --save @webkrafters/react-observable-context
14
-
15
- # Intro
16
-
17
- A context bearing an observable consumer [store](#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.
18
-
19
- **React::memo** *(and PureComponents)* remain the go-to solution for the repeated automatic re-renderings of entire component trees resulting from ***component*** state changes.
20
-
21
- ***Recommendation:*** For optimum performance, consider wrapping in **React::memo** most components using this module's ***useContext*** hook either directly or through another React hook. This will protect such components and their descendants from unrelated cascading render operations.
22
-
23
- ***Exempt*** from the recommendation are certain components such as those wrapped in the `React-Redux::connect()` Higher Order Component (HOC). Such HOC provides similar cascade-render protections to wrapped components and their descendants.
24
-
25
- # API
26
-
27
- The React-Observable-Context module contains **3** exports namely: the **createContext** function, the **useContext** hook and the **UsageError** class.
28
-
29
- * **createContext** is a zero-parameter function returning a store-bearing context. Pass the context as a `context` parameter to the [useContext()](#usecontext) to obtain the context's [store](#store).
30
-
31
- * <b id="usecontext">useContext</b> 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 using this hook.
32
-
33
- * **UsageError** class is the Error type reported for attempts to access this context's store outside of its Provider component tree.
34
-
35
- ## Provider
36
-
37
- The Provider component is a property of the `context`. It can immediately be used as-is anywhere the React-Observable-Context is required. It accepts the customary `children` and `value` props, and an optional `prehooks` prop <i>(discussed in the [prehooks](#prehooks) subsection below)</i>.
38
-
39
- ## Store
40
-
41
- The context's `store` exposes **4** methods for interacting with the context's internal state namely:
42
-
43
- * **getState**: (selector?: (state: State) => any) => any
44
-
45
- * **resetState**: VoidFunction // resets the state to the Provider initial `value` prop.
46
-
47
- * **setState**: (changes: PartialState\<State\>) => void // sets only new/changed state slices.\
48
- ***Do this:*** `setState({stateKey0: changes0[, ...]});`\
49
- ***Not this:*** `setState({stateKey0: {...state.stateKey0, ...changes0}[, ...]});`
50
-
51
- * **subscribe**: (listener: (newValue: PartialState\<State\>, oldValue: PartialState\<State\>) => void) => ***UnsubscribeFunction***
52
-
53
- ## Prehooks
54
-
55
- Prehooks provide a central place for sanitizing, modifying, transforming, validating etc. all related incoming state updates.
56
-
57
- The context store **2** update operations each adhere to its own user specified prehook when present. Otherwise, the update operation proceeds normally to completion. Thus, there are **2** prehooks named **resetState** and **setState** - after the store update methods they support.
58
-
59
- Each prehook returns a **boolean** value (`true` to continue AND `false` to abort the update operation). The prehook may modify (i.e. sanitize, transform, transpose) the argument to accurately reflect the intended update value. This is done by mutating part of the argument which holds the next `nextUpdate` values.
60
-
61
- * **resetState**: (state: {current: State, original: State}) => boolean // ***`state.original`*** holds the `nextUpdate` values.
62
-
63
- * **setState**: (newChanges: PartialState\<State\>) => boolean // ***`newChanges`*** holds the `nextUpdate` values.
64
-
65
- ***<u>Use case:</u>*** prehooks provide a central place for sanitizing, modifying, transforming, validating etc. all related incoming state updates.
66
-
67
- # Usage
68
-
69
- ### <u>*context.js*</u>
70
-
71
- import React from 'react';
72
- import { createContext, useContext } from '@webkrafters/react-observable-context';
73
- const ObservableContext = createContext();
74
- export const useObservableContext = watchedKeys => useContext( ObservableContext, watchedKeys );
75
- export default ObservableContext;
76
-
77
- ### <u>*index.js*</u>
78
-
79
- import React from 'react';
80
- import ReactDOM from 'react-dom';
81
- import App from './app';s
82
- ReactDOM.render(<App />, document.getElementById('root'));
83
-
84
- ### <u>*app.js*</u>
85
-
86
- import React, { useCallback, useState } from 'react';
87
- import Product from './product';
88
- const productUpdateHooks = {
89
- resetState: ( ...args ) => {
90
- console.log( 'resetting state with >>>> ', JSON.stringify( args ) );
91
- return true;
92
- },
93
- setState: ( ...args ) => {
94
- console.log( 'setting state with >>>> ', JSON.stringify( args ) );
95
- return true;
96
- }
97
- };
98
- const App = () => {
99
- const [ productType, setProductType ] = useState( 'Calculator' );
100
- const updateType = useCallback( e => setProductType( e.target.value ), [] );
101
- return (
102
- <div className="App">
103
- <h1>Demo</h1>
104
- <h2>A contrived product app.</h2>
105
- <div style={{ marginBottom: 10 }}>
106
- <label>Type: <input onKeyUp={ updateType } placeholder="override product type here..." /></label>
107
- </div>
108
- <Product
109
- type={ productType }
110
- updateHooks={ productUpdateHooks }
111
- />
112
- </div>
113
- );
114
- };
115
- App.displayName = 'App';
116
- export default App;
117
-
118
- ### <u>*product.js*</u>
119
-
120
- import React, { useCallback, useEffect, useState } from 'react';
121
- import ObservableContext from './context';
122
- import Editor from './editor';
123
- import PriceSticker from './price-sticker';
124
- import ProductDescription from './product-description';
125
- import TallyDisplay from './tally-display';
126
- const Product = ({ type, updateHooks }) => {
127
- const [ state, setState ] = useState(() => ({
128
- color: 'Burgundy',
129
- price: 22.5,
130
- type
131
- }));
132
- useEffect(() => {
133
- setState({ type }); // use this to update only the changed state value
134
- // setState({ ...state, type }); // this will override the context internal state for these values
135
- }, [ type ]);
136
- const overridePricing = useCallback( e => setState({ price: Number( e.target.value ) }), [] );
137
- return (
138
- <div>
139
- <div style={{ marginBottom: 10 }}>
140
- <label>$ <input onKeyUp={ overridePricing } placeholder="override price here..." /></label>
141
- </div>
142
- <ObservableContext.Provider
143
- prehooks={ updateHooks }
144
- value={ state }
145
- >
146
- <div style={{
147
- borderBottom: '1px solid #333',
148
- marginBottom: 10,
149
- paddingBottom: 5
150
- }}>
151
- <Editor />
152
- <TallyDisplay />
153
- </div>
154
- <ProductDescription />
155
- <PriceSticker />
156
- </ObservableContext.Provider>
157
- </div>
158
- );
159
- };
160
- Product.displayName = 'Product';
161
- export default Product;
162
-
163
- ### <u>*editor.js*</u>
164
-
165
- import React, { memo, useCallback, useEffect, useRef } from 'react';
166
- import { useObservableContext } from './context';
167
- const Editor = memo(() => {
168
- const { setState } = useObservableContext();
169
- const priceInputRef = useRef();
170
- const colorInputRef = useRef();
171
- const typeInputRef = useRef();
172
- const updatePrice = useCallback(() => {
173
- setState({ price: Number( priceInputRef.current.value ) });
174
- }, []);
175
- const updateColor = useCallback(() => {
176
- setState({ color: colorInputRef.current.value });
177
- }, []);
178
- const updateType = useCallback(() => {
179
- setState({ type: typeInputRef.current.value });
180
- }, []);
181
- useEffect(() => console.log( 'Editor component rendered.....' ));
182
- return (
183
- <fieldset style={{ margin: '10px 0' }}>
184
- <legend>Editor</legend>
185
- <div style={{ margin: '10px 0' }}>
186
- <label>New Price: <input ref={ priceInputRef } /></label>
187
- { ' ' }
188
- <button onClick={ updatePrice }>update price</button>
189
- </div>
190
- <div style={{ margin: '10px 0' }}>
191
- <label>New Color: <input ref={ colorInputRef } /></label>
192
- { ' ' }
193
- <button onClick={ updateColor }>update color</button>
194
- </div>
195
- <div style={{ margin: '10px 0' }}>
196
- <label>New Type: <input ref={ typeInputRef } /></label>
197
- { ' ' }
198
- <button onClick={ updateType }>update type</button>
199
- </div>
200
- </fieldset>
201
- );
202
- });
203
- Editor.displayName = 'Editor';
204
- export default Editor;
205
-
206
- ### <u>*price-sticker.js*</u>
207
-
208
- import React, { memo, useEffect } from 'react';
209
- import { useObservableContext } from './context';
210
- const CONTEXT_KEYS = [ 'price' ];
211
- const PriceSticker = memo(() => {
212
- const { getState } = useObservableContext( CONTEXT_KEYS );
213
- useEffect(() => console.log( 'PriceSticker component rendered.....' ));
214
- return (
215
- <div style={{ fontSize: 36, fontWeight: 800 }}>
216
- ${ getState( s => s.price ).toFixed( 2 ) }
217
- </div>
218
- );
219
- });
220
- PriceSticker.displayName = 'PriceSticker';
221
- export default PriceSticker;
222
-
223
- ### <u>*product-description.js*</u>
224
-
225
- import React, { memo, useEffect } from 'react';
226
- import { useObservableContext } from './context';
227
- const CONTEXT_KEYS = [ 'color', 'type' ];
228
- const ProductDescription = memo(() => {
229
- const store = useObservableContext( CONTEXT_KEYS );
230
- useEffect(() => console.log( 'ProductDescription component rendered.....' ));
231
- const color = store.getState( s => s.color );
232
- const type = store.getState( s => s.type );
233
- return (
234
- <div style={{ fontSize: 24 }}>
235
- <strong>Description:</strong> { color } { type }
236
- </div>
237
- );
238
- });
239
- ProductDescription.displayName = 'ProductDescription';
240
- export default ProductDescription;
241
-
242
- ### <u>*tally-display.js*</u>
243
-
244
- import React, { memo, useEffect } from 'react';
245
- import { useObservableContext } from './context';
246
- import Reset from './reset';
247
- const CONTEXT_KEYS = [ 'color', 'price', 'type' ];
248
- const TallyDisplay = memo(() => {
249
- const { getState } = useObservableContext( CONTEXT_KEYS );
250
- useEffect(() => console.log( 'TallyDisplay component rendered.....' ));
251
- return (
252
- <div>
253
- <table>
254
- <tbody>
255
- <tr><td><label>Type:</label></td><td>{ getState( s => s.type ) }</td></tr>
256
- <tr><td><label>Color:</label></td><td>{ getState( s => s.color ) }</td></tr>
257
- <tr><td><label>Price:</label></td><td>{ getState( s => s.price ).toFixed( 2 ) }</td></tr>
258
- </tbody>
259
- </table>
260
- <div style={{ textAlign: 'right' }}>
261
- <Reset />
262
- </div>
263
- </div>
264
- );
265
- });
266
- TallyDisplay.displayName = 'TallyDisplay';
267
- export default TallyDisplay;
268
-
269
- ### <u>*reset.js*</u>
270
-
271
- import React, { memo, useEffect } from 'react';
272
- import { useObservableContext } from './context';
273
- const Reset = memo(() => {
274
- const { resetState } = useObservableContext();
275
- useEffect(() => console.log( 'Reset component rendered.....' ));
276
- return ( <button onClick={ resetState }>reset context</button> );
277
- });
278
- Reset.displayName = 'Reset';
279
- export default Reset;
280
-
281
-
282
- # License
283
-
284
- MIT
1
+ # React-Observable-Context
2
+
3
+ A context bearing an observable consumer [store](#store).\
4
+ Only re-renders subscribing components on context state change.\
5
+ Subscribing component decides which context state properties' change to trigger its update.
6
+
7
+ **Name:** React-Observable-Context
8
+
9
+ **Usage:** Please see [Usage](#usage) section
10
+
11
+ **Install:**\
12
+ npm i -S @webkrafters/react-observable-context\
13
+ npm install --save @webkrafters/react-observable-context
14
+
15
+ # Intro
16
+
17
+ A context bearing an observable consumer [store](#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.
18
+
19
+ **React::memo** *(and PureComponents)* remain the go-to solution for the repeated automatic re-renderings of entire component trees resulting from ***component*** state changes.
20
+
21
+ ***Recommendation:*** For optimum performance, consider wrapping in **React::memo** most components using this module's ***useContext*** hook either directly or through another React hook. This will protect such components and their descendants from unrelated cascading render operations.
22
+
23
+ ***Exempt*** from the recommendation are certain components such as those wrapped in the `React-Redux::connect()` Higher Order Component (HOC). Such HOC provides similar cascade-render protections to wrapped components and their descendants.
24
+
25
+ # API
26
+
27
+ The React-Observable-Context module contains **3** exports namely: the **createContext** function, the **useContext** hook and the **UsageError** class.
28
+
29
+ * **createContext** is a zero-parameter function returning a store-bearing context. Pass the context as a `context` parameter to the [useContext()](#usecontext) to obtain the context's [store](#store).
30
+
31
+ * <b id="usecontext">useContext</b> 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 using this hook.
32
+
33
+ * **UsageError** class is the Error type reported for attempts to access this context's store outside of its Provider component tree.
34
+
35
+ ## Provider
36
+
37
+ The Provider component is a property of the `context`. It can immediately be used as-is anywhere the React-Observable-Context is required. It accepts the customary `children` and `value` props, and an optional `prehooks` prop <i>(discussed in the [prehooks](#prehooks) subsection below)</i>.
38
+
39
+ ## Store
40
+
41
+ The context's `store` exposes **4** methods for interacting with the context's internal state namely:
42
+
43
+ * **getState**: (selector?: (state: State) => any) => any
44
+
45
+ * **resetState**: VoidFunction // resets the state to the Provider initial `value` prop.
46
+
47
+ * **setState**: (changes: PartialState\<State\>) => void // merges only new/changed state slices.\
48
+ ***Do this:*** `setState({stateKey0: changes0[, ...]});`\
49
+ ***Not this:*** `setState({stateKey0: {...state.stateKey0, ...changes0}[, ...]});`
50
+
51
+ * **subscribe**: (listener: (newValue: PartialState\<State\>, oldValue: PartialState\<State\>) => void) => ***UnsubscribeFunction***
52
+
53
+ ## Prehooks
54
+
55
+ Prehooks provide a central place for sanitizing, modifying, transforming, validating etc. all related incoming state updates.
56
+
57
+ The context store **2** update operations each adhere to its own user specified prehook when present. Otherwise, the update operation proceeds normally to completion. Thus, there are **2** prehooks named **resetState** and **setState** - after the store update methods they support.
58
+
59
+ Each prehook returns a **boolean** value (`true` to continue AND `false` to abort the update operation). The prehook may modify (i.e. sanitize, transform, transpose) the argument to accurately reflect the intended update value. This is done by mutating part of the argument which holds the next `nextUpdate` values.
60
+
61
+ * **resetState**: (state: {current: State, original: State}) => boolean // ***`state.original`*** holds the `nextUpdate` values.
62
+
63
+ * **setState**: (newChanges: PartialState\<State\>) => boolean // ***`newChanges`*** holds the `nextUpdate` values.
64
+
65
+ ***<u>Use case:</u>*** prehooks provide a central place for sanitizing, modifying, transforming, validating etc. all related incoming state updates.
66
+
67
+ # Usage
68
+
69
+ ### <u>*context.js*</u>
70
+
71
+ import React from 'react';
72
+ import { createContext, useContext } from '@webkrafters/react-observable-context';
73
+ const ObservableContext = createContext();
74
+ export const useObservableContext = watchedKeys => useContext( ObservableContext, watchedKeys );
75
+ export default ObservableContext;
76
+
77
+ ### <u>*index.js*</u>
78
+
79
+ import React from 'react';
80
+ import ReactDOM from 'react-dom';
81
+ import App from './app';
82
+ ReactDOM.render(<App />, document.getElementById('root'));
83
+
84
+ ### <u>*app.js*</u>
85
+
86
+ import React, { useCallback, useState } from 'react';
87
+ import Product from './product';
88
+ const productUpdateHooks = {
89
+ resetState: ( ...args ) => {
90
+ console.log( 'resetting state with >>>> ', JSON.stringify( args ) );
91
+ return true;
92
+ },
93
+ setState: ( ...args ) => {
94
+ console.log( 'merging following into state >>>> ', JSON.stringify( args ) );
95
+ return true;
96
+ }
97
+ };
98
+ const App = () => {
99
+ const [ productType, setProductType ] = useState( 'Calculator' );
100
+ const updateType = useCallback( e => setProductType( e.target.value ), [] );
101
+ return (
102
+ <div className="App">
103
+ <h1>Demo</h1>
104
+ <h2>A contrived product app.</h2>
105
+ <div style={{ marginBottom: 10 }}>
106
+ <label>Type: <input onKeyUp={ updateType } placeholder="override product type here..." /></label>
107
+ </div>
108
+ <Product
109
+ type={ productType }
110
+ updateHooks={ productUpdateHooks }
111
+ />
112
+ </div>
113
+ );
114
+ };
115
+ App.displayName = 'App';
116
+ export default App;
117
+
118
+ ### <u>*product.js*</u>
119
+
120
+ import React, { useCallback, useEffect, useState } from 'react';
121
+ import ObservableContext from './context';
122
+ import Editor from './editor';
123
+ import PriceSticker from './price-sticker';
124
+ import ProductDescription from './product-description';
125
+ import TallyDisplay from './tally-display';
126
+ const Product = ({ type, updateHooks }) => {
127
+ const [ state, setState ] = useState(() => ({
128
+ color: 'Burgundy',
129
+ price: 22.5,
130
+ type
131
+ }));
132
+ useEffect(() => {
133
+ setState({ type }); // use this to update only the changed state value
134
+ // setState({ ...state, type }); // this will override the context internal state for these values
135
+ }, [ type ]);
136
+ const overridePricing = useCallback( e => setState({ price: Number( e.target.value ) }), [] );
137
+ return (
138
+ <div>
139
+ <div style={{ marginBottom: 10 }}>
140
+ <label>$ <input onKeyUp={ overridePricing } placeholder="override price here..." /></label>
141
+ </div>
142
+ <ObservableContext.Provider
143
+ prehooks={ updateHooks }
144
+ value={ state }
145
+ >
146
+ <div style={{
147
+ borderBottom: '1px solid #333',
148
+ marginBottom: 10,
149
+ paddingBottom: 5
150
+ }}>
151
+ <Editor />
152
+ <TallyDisplay />
153
+ </div>
154
+ <ProductDescription />
155
+ <PriceSticker />
156
+ </ObservableContext.Provider>
157
+ </div>
158
+ );
159
+ };
160
+ Product.displayName = 'Product';
161
+ export default Product;
162
+
163
+ ### <u>*editor.js*</u>
164
+
165
+ import React, { memo, useCallback, useEffect, useRef } from 'react';
166
+ import { useObservableContext } from './context';
167
+ const Editor = memo(() => {
168
+ const { setState } = useObservableContext();
169
+ const priceInputRef = useRef();
170
+ const colorInputRef = useRef();
171
+ const typeInputRef = useRef();
172
+ const updatePrice = useCallback(() => {
173
+ setState({ price: Number( priceInputRef.current.value ) });
174
+ }, []);
175
+ const updateColor = useCallback(() => {
176
+ setState({ color: colorInputRef.current.value });
177
+ }, []);
178
+ const updateType = useCallback(() => {
179
+ setState({ type: typeInputRef.current.value });
180
+ }, []);
181
+ useEffect(() => console.log( 'Editor component rendered.....' ));
182
+ return (
183
+ <fieldset style={{ margin: '10px 0' }}>
184
+ <legend>Editor</legend>
185
+ <div style={{ margin: '10px 0' }}>
186
+ <label>New Price: <input ref={ priceInputRef } /></label>
187
+ { ' ' }
188
+ <button onClick={ updatePrice }>update price</button>
189
+ </div>
190
+ <div style={{ margin: '10px 0' }}>
191
+ <label>New Color: <input ref={ colorInputRef } /></label>
192
+ { ' ' }
193
+ <button onClick={ updateColor }>update color</button>
194
+ </div>
195
+ <div style={{ margin: '10px 0' }}>
196
+ <label>New Type: <input ref={ typeInputRef } /></label>
197
+ { ' ' }
198
+ <button onClick={ updateType }>update type</button>
199
+ </div>
200
+ </fieldset>
201
+ );
202
+ });
203
+ Editor.displayName = 'Editor';
204
+ export default Editor;
205
+
206
+ ### <u>*price-sticker.js*</u>
207
+
208
+ import React, { memo, useEffect } from 'react';
209
+ import { useObservableContext } from './context';
210
+ const CONTEXT_KEYS = [ 'price' ];
211
+ const PriceSticker = memo(() => {
212
+ const { getState } = useObservableContext( CONTEXT_KEYS );
213
+ useEffect(() => console.log( 'PriceSticker component rendered.....' ));
214
+ return (
215
+ <div style={{ fontSize: 36, fontWeight: 800 }}>
216
+ ${ getState( s => s.price ).toFixed( 2 ) }
217
+ </div>
218
+ );
219
+ });
220
+ PriceSticker.displayName = 'PriceSticker';
221
+ export default PriceSticker;
222
+
223
+ ### <u>*product-description.js*</u>
224
+
225
+ import React, { memo, useEffect } from 'react';
226
+ import { useObservableContext } from './context';
227
+ const CONTEXT_KEYS = [ 'color', 'type' ];
228
+ const ProductDescription = memo(() => {
229
+ const store = useObservableContext( CONTEXT_KEYS );
230
+ useEffect(() => console.log( 'ProductDescription component rendered.....' ));
231
+ const color = store.getState( s => s.color );
232
+ const type = store.getState( s => s.type );
233
+ return (
234
+ <div style={{ fontSize: 24 }}>
235
+ <strong>Description:</strong> { color } { type }
236
+ </div>
237
+ );
238
+ });
239
+ ProductDescription.displayName = 'ProductDescription';
240
+ export default ProductDescription;
241
+
242
+ ### <u>*tally-display.js*</u>
243
+
244
+ import React, { memo, useEffect } from 'react';
245
+ import { useObservableContext } from './context';
246
+ import Reset from './reset';
247
+ const CONTEXT_KEYS = [ 'color', 'price', 'type' ];
248
+ const TallyDisplay = memo(() => {
249
+ const { getState } = useObservableContext( CONTEXT_KEYS );
250
+ useEffect(() => console.log( 'TallyDisplay component rendered.....' ));
251
+ return (
252
+ <div>
253
+ <table>
254
+ <tbody>
255
+ <tr><td><label>Type:</label></td><td>{ getState( s => s.type ) }</td></tr>
256
+ <tr><td><label>Color:</label></td><td>{ getState( s => s.color ) }</td></tr>
257
+ <tr><td><label>Price:</label></td><td>{ getState( s => s.price ).toFixed( 2 ) }</td></tr>
258
+ </tbody>
259
+ </table>
260
+ <div style={{ textAlign: 'right' }}>
261
+ <Reset />
262
+ </div>
263
+ </div>
264
+ );
265
+ });
266
+ TallyDisplay.displayName = 'TallyDisplay';
267
+ export default TallyDisplay;
268
+
269
+ ### <u>*reset.js*</u>
270
+
271
+ import React, { memo, useEffect } from 'react';
272
+ import { useObservableContext } from './context';
273
+ const Reset = memo(() => {
274
+ const { resetState } = useObservableContext();
275
+ useEffect(() => console.log( 'Reset component rendered.....' ));
276
+ return ( <button onClick={ resetState }>reset context</button> );
277
+ });
278
+ Reset.displayName = 'Reset';
279
+ export default Reset;
280
+
281
+
282
+ # License
283
+
284
+ MIT
package/dist/index.js CHANGED
@@ -249,49 +249,25 @@ var memoizeImmediateChildTree = function memoizeImmediateChildTree(children) {
249
249
  });
250
250
  };
251
251
 
252
- /**
253
- * @type {FC<{
254
- * children?: ReactNode,
255
- * provider: ObservableContext<T>["Provider"],
256
- * value: Store<T>
257
- * }>}
258
- * @template {State} T
259
- */
260
- var ProviderInternal = function ProviderInternal(_ref3) {
261
- var children = _ref3.children,
262
- Provider = _ref3.provider,
263
- value = _ref3.value;
264
- return /*#__PURE__*/_react["default"].createElement(Provider, {
265
- value: value
266
- }, memoizeImmediateChildTree(children));
267
- };
268
- ProviderInternal.displayName = 'ObservableContext.Provider.Internal';
269
-
270
- /**
271
- * @param {Provider<Store<T>>} Provider
272
- */
252
+ /** @param {Provider<Store<T>>} Provider */
273
253
  var makeObservable = function makeObservable(Provider) {
274
254
  /**
275
- * Note: `context` prop is not updateable. Furtther updates to this prop are ignored.
276
- *
277
255
  * @type {FC<{
278
256
  * children?: ReactNode,
279
- * context: ObservableContext<T>,
280
257
  * prehooks?: Prehooks<T>
281
258
  * value: PartialState<T>
282
259
  * }>}
283
260
  * @template {State} T
284
261
  */
285
- var Observable = function Observable(_ref4) {
286
- var _ref4$children = _ref4.children,
287
- children = _ref4$children === void 0 ? null : _ref4$children,
288
- _ref4$prehooks = _ref4.prehooks,
289
- prehooks = _ref4$prehooks === void 0 ? defaultPrehooks : _ref4$prehooks,
290
- value = _ref4.value;
291
- return /*#__PURE__*/_react["default"].createElement(ProviderInternal, {
292
- provider: Provider,
262
+ var Observable = function Observable(_ref3) {
263
+ var _ref3$children = _ref3.children,
264
+ children = _ref3$children === void 0 ? null : _ref3$children,
265
+ _ref3$prehooks = _ref3.prehooks,
266
+ prehooks = _ref3$prehooks === void 0 ? defaultPrehooks : _ref3$prehooks,
267
+ value = _ref3.value;
268
+ return /*#__PURE__*/_react["default"].createElement(Provider, {
293
269
  value: useStore(prehooks, value)
294
- }, children);
270
+ }, memoizeImmediateChildTree(children));
295
271
  };
296
272
  Observable.displayName = 'ObservableContext.Provider';
297
273
  return Observable;
package/package.json CHANGED
@@ -81,5 +81,5 @@
81
81
  "test:watch": "eslint --fix && jest --updateSnapshot --watchAll"
82
82
  },
83
83
  "types": "dist/index.d.ts",
84
- "version": "3.0.0-rc.0"
84
+ "version": "3.0.0"
85
85
  }