@t8/react-store 1.1.1 → 1.1.3

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 CHANGED
@@ -174,3 +174,13 @@ The ready-to-use hook from the [T8 React Pending](https://github.com/t8js/react-
174
174
  ## Remount-persistent state
175
175
 
176
176
  A standalone store initialized outside a component can be used by the component as remount-persistent state, whether used by other components or not.
177
+
178
+ ## Persistence across page reloads
179
+
180
+ ```js
181
+ import { Store, persist } from "@t8/react-store";
182
+
183
+ let counterStore = persist(new Store(0), "counter");
184
+ ```
185
+
186
+ Whenever it's updated, `counterStore` above will save its state to the `"counter"` key of `localStorage`. (Pass `true` as the third parameter of `persist()` to use `sessionStorage` instead of `localStorage`.) `counterStore` returned from `persist()` is the same store passed as the first parameter enhanced to be persistent across page reloads.
package/dist/index.js CHANGED
@@ -10,16 +10,20 @@ function getStorage(session) {
10
10
  }
11
11
  function persist(store, storageKey, session = false) {
12
12
  let inited = false;
13
- function read() {
13
+ function read(state) {
14
14
  let storage = getStorage(session);
15
+ let rawState = null;
15
16
  if (storage) {
16
17
  try {
17
- let rawState = storage.getItem(storageKey);
18
+ rawState = storage.getItem(storageKey);
18
19
  if (rawState !== null) store.setState(JSON.parse(rawState));
19
20
  } catch {
20
21
  }
21
22
  }
22
- if (!inited) inited = true;
23
+ if (!inited) {
24
+ inited = true;
25
+ if (rawState === null) write(state);
26
+ }
23
27
  }
24
28
  function write(state) {
25
29
  let storage = getStorage(session);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@t8/react-store",
3
- "version": "1.1.1",
3
+ "version": "1.1.3",
4
4
  "description": "Small React app state management lib aligned with React's state pattern, condensed to the essentials",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -45,6 +45,6 @@
45
45
  "typescript": "^5.9.3"
46
46
  },
47
47
  "dependencies": {
48
- "@t8/store": "^1.2.1"
48
+ "@t8/store": "^1.2.3"
49
49
  }
50
50
  }