@dr.pogodin/react-global-state 0.6.4 → 0.6.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 +77 -0
- package/package.json +20 -19
package/README.md
CHANGED
|
@@ -257,5 +257,82 @@ of some, or all async data at the server side.
|
|
|
257
257
|
}
|
|
258
258
|
```
|
|
259
259
|
|
|
260
|
+
### Frequently Asked Questions
|
|
261
|
+
|
|
262
|
+
- _Does React Global State library avoid unnecessary component re-renders when values updated in the global state are irrelevant to those components?_
|
|
263
|
+
|
|
264
|
+
Yes, it does avoid unnecessary re-renders of the component tree. A component
|
|
265
|
+
relying on `some.path` in the global state is re-rendered only when the value
|
|
266
|
+
at this path, or its sub-path has changed; _i.e._ it will be re-rendered if
|
|
267
|
+
the value at `some.path` has changed, and it will be re-rendered if the value
|
|
268
|
+
at `some.path.sub.path` has changed.
|
|
269
|
+
|
|
270
|
+
- _How would you describe your use case compared to another React global state library, e.g. [Valtio](https://www.npmjs.com/package/valtio)?_
|
|
271
|
+
|
|
272
|
+
1. React Global State is designed to follow the standard React API as close
|
|
273
|
+
as possible. _E.g._ if some component relies on the local state:
|
|
274
|
+
```jsx
|
|
275
|
+
const [value, setValue] = useState(initialState);
|
|
276
|
+
```
|
|
277
|
+
to move that value to the global state (or _vice versa_) one only needs to
|
|
278
|
+
replace the hook with
|
|
279
|
+
```jsx
|
|
280
|
+
const [value, setValue] = useGlobalState(path, initialState);
|
|
281
|
+
```
|
|
282
|
+
The [useGlobalState()] hook takes care to follow all edge cases of the
|
|
283
|
+
standard [useState()]: `setValue` setter identity is stable (does not
|
|
284
|
+
change on re-renders), functional updates and lazy initial state are
|
|
285
|
+
supported.
|
|
286
|
+
|
|
287
|
+
Other libraries tend to re-invent the wheel, introducing their own APIs,
|
|
288
|
+
which (i) should be learned and understood; (ii) do complicate migration
|
|
289
|
+
of components between the local and global state, should it be needed in
|
|
290
|
+
a course of app development / prototyping.
|
|
291
|
+
|
|
292
|
+
2. When it comes to async data in the global state other libraries tend to
|
|
293
|
+
offer only a very basic supported, often relying on experimental or internal
|
|
294
|
+
React mechanics.
|
|
295
|
+
|
|
296
|
+
React Global State, [useAsyncData()] and [useAsyncCollection()] hooks in
|
|
297
|
+
particular, implements async data fetching and management features: when
|
|
298
|
+
multiple components use these hooks to load async data to the same global
|
|
299
|
+
state path the library takes care to do the actual loading just once, and
|
|
300
|
+
then keep the data without reloading until their age reaches (configurable)
|
|
301
|
+
max age. There is an automated garbage collection of expired, non-used
|
|
302
|
+
async data from the global state; there is server-side rendering (SSR)
|
|
303
|
+
support, with suggested high-level setup taking care that all async data
|
|
304
|
+
loaded using [useAsyncData()] and [useAsyncCollection()] hooks will be
|
|
305
|
+
automatically loaded and used in server-side renders (still allowing to
|
|
306
|
+
opt-out of that for individual hooks, and timeout server-side fetching of
|
|
307
|
+
data that take too long to arrive, in which case the library will fetch
|
|
308
|
+
such data client-side). It does not rely on experimental React APIs to
|
|
309
|
+
achieve its functionality, it only uses current public APIs.
|
|
310
|
+
|
|
311
|
+
For me the support of async data fetching into the global state and their
|
|
312
|
+
further management with out-of-the-box SSR support was the primary
|
|
313
|
+
motivation to create React Global State. There are many other global state
|
|
314
|
+
React libraries, but I was not able to find any that would cover the async
|
|
315
|
+
data handling with that ease I believed was possible. The secondary
|
|
316
|
+
motivation was that existing global state libraries either had
|
|
317
|
+
the shortcoming of unnecessary component re-renders when data irrelevant
|
|
318
|
+
to them where updated in the global state, or introduced their
|
|
319
|
+
own APIs, where following the standard React APIs for local state looks
|
|
320
|
+
to me a way more convenient approach.
|
|
321
|
+
|
|
322
|
+
- _Is React Global State library production ready (considering the current version number 0.y.z)?_
|
|
323
|
+
|
|
324
|
+
Yes. I personally use it in production for all my commercial and personal
|
|
325
|
+
React projects for over an year. I just don't feel like to call it v1 until
|
|
326
|
+
a reasonable adoption by 3rd party developers, and any API improvements that
|
|
327
|
+
may come out of community experience.
|
|
328
|
+
|
|
260
329
|
[Library Reference](https://dr.pogodin.studio/docs/react-global-state/index.html) •
|
|
261
330
|
[Blog Article](https://dr.pogodin.studio/dev-blog/the-global-state-in-react-designed-right)
|
|
331
|
+
|
|
332
|
+
[useAsyncCollection()]: https://dr.pogodin.studio/docs/react-global-state/docs/api/hooks/useasynccollection
|
|
333
|
+
[useAsyncData()]: https://dr.pogodin.studio/docs/react-global-state/docs/api/hooks/useasyncdata
|
|
334
|
+
[useGlobalState()]: https://dr.pogodin.studio/docs/react-global-state/docs/api/hooks/useglobalstate
|
|
335
|
+
[useState()]: https://reactjs.org/docs/hooks-reference.html#usestate
|
|
336
|
+
|
|
337
|
+
[functional updates]: https://reactjs.org/docs/hooks-reference.html#functional-updates
|
|
338
|
+
[lazy initial state]: https://reactjs.org/docs/hooks-reference.html#functional-updates
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dr.pogodin/react-global-state",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.5",
|
|
4
4
|
"description": "Hook-based global state for React",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -27,31 +27,32 @@
|
|
|
27
27
|
"bugs": {
|
|
28
28
|
"url": "https://github.com/birdofpreyru/react-global-state.git/issues"
|
|
29
29
|
},
|
|
30
|
-
"homepage": "https://dr.pogodin.studio/
|
|
30
|
+
"homepage": "https://dr.pogodin.studio/docs/react-global-state/index.html",
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@babel/runtime": "^7.
|
|
32
|
+
"@babel/runtime": "^7.16.3",
|
|
33
33
|
"lodash": "^4.17.21",
|
|
34
34
|
"uuid": "^8.3.2"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
|
-
"@babel/cli": "^7.
|
|
38
|
-
"@babel/core": "^7.
|
|
39
|
-
"@babel/
|
|
40
|
-
"@babel/plugin
|
|
41
|
-
"@babel/
|
|
42
|
-
"@babel/
|
|
43
|
-
"babel-
|
|
44
|
-
"babel-
|
|
37
|
+
"@babel/cli": "^7.16.0",
|
|
38
|
+
"@babel/core": "^7.16.0",
|
|
39
|
+
"@babel/eslint-parser": "^7.16.3",
|
|
40
|
+
"@babel/eslint-plugin": "^7.14.5",
|
|
41
|
+
"@babel/node": "^7.16.0",
|
|
42
|
+
"@babel/plugin-transform-runtime": "^7.16.4",
|
|
43
|
+
"@babel/preset-env": "^7.16.4",
|
|
44
|
+
"@babel/preset-react": "^7.16.0",
|
|
45
|
+
"babel-jest": "^27.4.2",
|
|
45
46
|
"babel-plugin-module-resolver": "^4.1.0",
|
|
46
|
-
"eslint": "^
|
|
47
|
-
"eslint-config-airbnb": "^
|
|
47
|
+
"eslint": "^8.4.1",
|
|
48
|
+
"eslint-config-airbnb": "^19.0.2",
|
|
48
49
|
"eslint-import-resolver-babel-module": "^5.3.1",
|
|
49
|
-
"eslint-plugin-import": "^2.25.
|
|
50
|
-
"eslint-plugin-jest": "^25.
|
|
51
|
-
"eslint-plugin-jsx-a11y": "^6.
|
|
52
|
-
"eslint-plugin-react": "^7.
|
|
53
|
-
"eslint-plugin-react-hooks": "^4.
|
|
54
|
-
"jest": "^27.3
|
|
50
|
+
"eslint-plugin-import": "^2.25.3",
|
|
51
|
+
"eslint-plugin-jest": "^25.3.0",
|
|
52
|
+
"eslint-plugin-jsx-a11y": "^6.5.1",
|
|
53
|
+
"eslint-plugin-react": "^7.27.1",
|
|
54
|
+
"eslint-plugin-react-hooks": "^4.3.0",
|
|
55
|
+
"jest": "^27.4.3",
|
|
55
56
|
"jsdoc": "^3.6.7",
|
|
56
57
|
"mockdate": "^3.0.5",
|
|
57
58
|
"pretty": "^2.0.0",
|