@t8/react-store 0.1.0 → 1.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 (2) hide show
  1. package/README.md +104 -0
  2. package/package.json +4 -4
package/README.md CHANGED
@@ -0,0 +1,104 @@
1
+ [![npm](https://flat.badgen.net/npm/v/@t8/react-store?labelColor=345&color=46e)](https://www.npmjs.com/package/@t8/react-store) ![Lightweight](https://flat.badgen.net/bundlephobia/minzip/@t8/react-store/?label=minzip&labelColor=345&color=46e) ![TypeScript ✓](https://flat.badgen.net/badge/TypeScript/✓?labelColor=345&color=345) ![CSR ✓](https://flat.badgen.net/badge/CSR/✓?labelColor=345&color=345) ![SSR ✓](https://flat.badgen.net/badge/SSR/✓?labelColor=345&color=345)
2
+
3
+ # @t8/react-store
4
+
5
+ *Straightforward and minimalist shared state management for React apps*
6
+
7
+ - Similar to `useState()`
8
+ - No boilerplate
9
+ - Painless transition from local state to shared state and vice versa
10
+ - SSR-compatible
11
+
12
+ Installation: `npm i @t8/react-store`
13
+
14
+ ## Usage
15
+
16
+ Moving the local state to the full-fledged shared state:
17
+
18
+ ```diff
19
+ import {createContext, useContext} from 'react';
20
+ + import {Store, useStore} from '@t8/react-store';
21
+ +
22
+ + let AppContext = createContext(new Store(0));
23
+
24
+ let Counter = () => {
25
+ - let [counter, setCounter] = useState(0);
26
+ + let [counter, setCounter] = useStore(useContext(AppContext));
27
+
28
+ let handleClick = () => {
29
+ setCounter(value => value + 1);
30
+ };
31
+
32
+ return <button onClick={handleClick}>{counter}</button>;
33
+ };
34
+
35
+ let ResetButton = () => {
36
+ - let [, setCounter] = useState(0);
37
+ + let [, setCounter] = useStore(useContext(AppContext), false);
38
+
39
+ let handleClick = () => {
40
+ setCounter(0);
41
+ };
42
+
43
+ return <button onClick={handleClick}>×</button>;
44
+ };
45
+
46
+ let App = () => <><Counter/>{' '}<ResetButton/></>;
47
+ ```
48
+
49
+ [Live demo](https://codesandbox.io/p/sandbox/rtng37?file=%2Fsrc%2FPlusButton.jsx)
50
+
51
+ 🔹 The shared state setup with `@t8/react-store` is very similar to `useState()` allowing for quick migration from local state to shared state or the other way around.
52
+
53
+ 🔹 The `false` parameter in `useStore(store, false)` (as in `<ResetButton>` above) tells the hook not to subscribe the component to tracking the store state updates. The common use case is when the component doesn't make use of the store state value, but it may use the state setter.
54
+
55
+ 🔹 An application can have as many stores as needed, whether on a single React Context or multiple Contexts.
56
+
57
+ ```js
58
+ let AppContext = createContext({
59
+ users: new Store(/* ... */),
60
+ items: new Store(/* ... */),
61
+ });
62
+ ```
63
+
64
+ 🔹 Apart from a boolean, `useStore(store, shouldUpdate)` can take a function of `(nextState, prevState) => boolean` as the second parameter to filter store updates to respond to:
65
+
66
+ ```jsx
67
+ let ItemCard = ({id}) => {
68
+ let hasRelevantUpdates = useCallback((nextItems, prevItems) => {
69
+ return nextItems[id].revision !== prevItems[id].revision;
70
+ }, [id]);
71
+
72
+ let [items, setItems] = useStore(
73
+ useContext(AppContext).items,
74
+ hasRelevantUpdates,
75
+ );
76
+
77
+ return (
78
+ // content
79
+ );
80
+ };
81
+ ```
82
+
83
+ 🔹 Shared state can be provided to the app by means of a regular React Context provider:
84
+
85
+ ```diff
86
+ let App = () => (
87
+ - <AppContext.Provider value={42}>
88
+ + <AppContext.Provider value={new Store(42)}>
89
+ <PlusButton/>{' '}<Display/>
90
+ </AppContext.Provider>
91
+ );
92
+ ```
93
+
94
+ 🔹 A store can contain data of any type.
95
+
96
+ Live demos:<br>
97
+ [Primitive value state](https://codesandbox.io/p/sandbox/rtng37?file=%2Fsrc%2FPlusButton.jsx)<br>
98
+ [Object value state](https://codesandbox.io/p/sandbox/y7wt2j?file=%2Fsrc%2FPlusButton.jsx)
99
+
100
+ 🔹 Immer can be used with `useStore()` just the same way as [with `useState()`](https://immerjs.github.io/immer/example-setstate#usestate--immer) to facilitate deeply nested data changes.
101
+
102
+ [Live demo with Immer](https://codesandbox.io/p/sandbox/rn4qsr?file=%2Fsrc%2FPlusButton.jsx)
103
+
104
+ 🔹 A store initialized outside a component can be used as the component's remount-persistent state.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@t8/react-store",
3
- "version": "0.1.0",
3
+ "version": "1.0.0",
4
4
  "description": "Straightforward and minimalist shared state management for React apps",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -26,10 +26,10 @@
26
26
  "peerDependencies": {
27
27
  "react": ">=16.8"
28
28
  },
29
- "dependencies": {
30
- "@t8/store": "^0.1.1"
31
- },
32
29
  "devDependencies": {
33
30
  "@types/react": "^19.1.10"
31
+ },
32
+ "dependencies": {
33
+ "@t8/store": "^1.0.0"
34
34
  }
35
35
  }