@webkrafters/react-observable-context 4.1.8 → 4.2.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.
- package/README.md +60 -20
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -86,6 +86,48 @@ A context bearing an observable consumer [store](#store). State changes within t
|
|
|
86
86
|
|
|
87
87
|
<h1 id="getting-started">Getting Started</h1>
|
|
88
88
|
|
|
89
|
+
<i><b><u>index.js</u></b></i>
|
|
90
|
+
|
|
91
|
+
```jsx
|
|
92
|
+
import React from 'react';
|
|
93
|
+
import ReactDOM from 'react-dom';
|
|
94
|
+
import App from './app';
|
|
95
|
+
|
|
96
|
+
ReactDOM.render( <App />, document.getElementById( 'root' ) );
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
<i><b><u>app.js</u></b></i>
|
|
100
|
+
|
|
101
|
+
```jsx
|
|
102
|
+
import React, { useEffect, useState } from 'react';
|
|
103
|
+
import ProviderDeno from './provider-demo';
|
|
104
|
+
|
|
105
|
+
const MILLIS_PER_MINUTE = 6e4;
|
|
106
|
+
|
|
107
|
+
let numCreated = 0;
|
|
108
|
+
|
|
109
|
+
const App = () => {
|
|
110
|
+
const [ age, updateAge ] = useState( 0 );
|
|
111
|
+
const [ testNumber ] = useState( () => ++numCreated );
|
|
112
|
+
|
|
113
|
+
useEffect(() => {
|
|
114
|
+
const t = setTimeout(
|
|
115
|
+
() => updateAge( age => age + 1 ),
|
|
116
|
+
MILLIS_PER_MINUTE
|
|
117
|
+
);
|
|
118
|
+
return () => clearTimeout( t );
|
|
119
|
+
}, [ age ]);
|
|
120
|
+
|
|
121
|
+
return (
|
|
122
|
+
<div>
|
|
123
|
+
<h2>App instance #: { testNumber }</H2>
|
|
124
|
+
<ProviderDeno ageInMinutes={ age } />
|
|
125
|
+
</div>
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
export default App;
|
|
129
|
+
```
|
|
130
|
+
|
|
89
131
|
<i id="create-context-usage"><u><b>context.js</b></u></i>
|
|
90
132
|
|
|
91
133
|
```jsx
|
|
@@ -93,25 +135,27 @@ import { createContext } from '@webkrafters/react-observable-context';
|
|
|
93
135
|
export default createContext();
|
|
94
136
|
```
|
|
95
137
|
|
|
96
|
-
<i id="provider-usage"><b><u>provider.js</u></b></i>
|
|
138
|
+
<i id="provider-usage"><b><u>provider-demo.js</u></b></i>
|
|
97
139
|
|
|
98
140
|
```jsx
|
|
99
141
|
import React, { useEffect, useState } from 'react';
|
|
100
142
|
import ObservableContext from './context';
|
|
101
143
|
import Ui from './ui';
|
|
102
144
|
|
|
103
|
-
const
|
|
145
|
+
const createInitialState = c = ({
|
|
146
|
+
a: { b: { c, x: { y: { z: [ 2022 ] } } } }
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
const ProviderDemo = ({ ageInMinutes: c = 0 }) => {
|
|
104
150
|
|
|
105
|
-
const [ state, setState ] = useState(() => (
|
|
106
|
-
a: { b: { c, x: { y: { z: [ 2022 ] } } } }
|
|
107
|
-
}));
|
|
151
|
+
const [ state, setState ] = useState(() => createInitialState( c ));
|
|
108
152
|
|
|
109
153
|
useEffect(() => {
|
|
110
154
|
// similar to `store.setState`, use the following to update
|
|
111
155
|
// only the changed slice of the context internal state.
|
|
112
|
-
|
|
156
|
+
// Please see `Set State` section
|
|
113
157
|
setState({ a: { b: { c } } }); // OR
|
|
114
|
-
|
|
158
|
+
// setState({ a: { b: { c: { '@@REPLACE': c } } } });
|
|
115
159
|
// Do not do the following: it will override the context internal state.
|
|
116
160
|
// setState({ ...state, a: { ...state.a, b: { ...state.a.b, c } } });
|
|
117
161
|
}, [ c ]);
|
|
@@ -122,9 +166,9 @@ const Provider = ({ c = 36 }) => {
|
|
|
122
166
|
</ObservableContext.Provider>
|
|
123
167
|
);
|
|
124
168
|
};
|
|
125
|
-
|
|
169
|
+
ProviderDemo.displayName = 'ProviderDemo';
|
|
126
170
|
|
|
127
|
-
export default
|
|
171
|
+
export default ProviderDemo;
|
|
128
172
|
```
|
|
129
173
|
|
|
130
174
|
<i id="connect-usage"><u><b>ui.js</b></u> (connect method)</i>
|
|
@@ -135,13 +179,16 @@ import { connect } from '@webkrafters/react-observable-context';
|
|
|
135
179
|
import ObservableContext from './context';
|
|
136
180
|
|
|
137
181
|
export const YearText = ({ data }) => ( <div>Year: { data.year }</div> );
|
|
138
|
-
|
|
182
|
+
|
|
183
|
+
export const YearInput = ({ data, resetState, setState }) => {
|
|
139
184
|
const onChange = useCallback( e => setState({
|
|
140
185
|
a: { b: { x: { y: { z: { 0: e.target.value } } } } }
|
|
141
186
|
}), [ setState ]);
|
|
187
|
+
|
|
142
188
|
useEffect(() => {
|
|
143
189
|
data.year > 2049 && resetState([ 'a.b.c' ]);
|
|
144
190
|
}, [ data.year ]);
|
|
191
|
+
|
|
145
192
|
return ( <div>Year: <input type="number" onChange={ onChange } /> );
|
|
146
193
|
};
|
|
147
194
|
|
|
@@ -175,12 +222,15 @@ const Client1 = memo(() => { // memoize to prevent 'no-change' renders from the
|
|
|
175
222
|
|
|
176
223
|
const Client2 = memo(() => { // memoize to prevent 'no-change' renders from the parent.
|
|
177
224
|
const { data, setState, resetState } = useContext( ObservableContext, selectorMap );
|
|
225
|
+
|
|
178
226
|
const onChange = useCallback( e => setState({
|
|
179
227
|
a: { b: { x: { y: { z: { 0: e.target.value } } } } }
|
|
180
228
|
}), [ setState ]);
|
|
229
|
+
|
|
181
230
|
useEffect(() => {
|
|
182
231
|
data.year > 2049 && resetState([ 'a.b.c' ]);
|
|
183
232
|
}, [ data.year ]);
|
|
233
|
+
|
|
184
234
|
return ( <div>Year: <input type="number" onChange={ onChange } /> );
|
|
185
235
|
});
|
|
186
236
|
|
|
@@ -194,16 +244,6 @@ const Ui = () => (
|
|
|
194
244
|
export default Ui;
|
|
195
245
|
```
|
|
196
246
|
|
|
197
|
-
<i><b><u>index.js</u></b></i>
|
|
198
|
-
|
|
199
|
-
```jsx
|
|
200
|
-
import React from 'react';
|
|
201
|
-
import ReactDOM from 'react-dom';
|
|
202
|
-
import Provider from './provider';
|
|
203
|
-
|
|
204
|
-
ReactDOM.render( <Provider />, document.getElementById( 'root' ) );
|
|
205
|
-
```
|
|
206
|
-
|
|
207
247
|
# API
|
|
208
248
|
|
|
209
249
|
The React-Observable-Context module exports named constants and the following **4** main entities namely:
|
package/package.json
CHANGED