@t8/react-store 1.2.1 → 1.2.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
@@ -68,6 +68,8 @@ Moving the local state to the full-fledged shared state:
68
68
 
69
69
  🔹 The optional `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 a component makes use of the store state setter without using the store state value.
70
70
 
71
+ 🔹 With SSR, it's common practice to put shared values into React Context rather than module-level variables to avoid cross-request data sharing. The same applies to stores, see an example in the [Sharing state via Context](#sharing-state-via-context) section below.
72
+
71
73
  🔹 Similarly to instances of the built-in data container classes, such as `Set` and `Map`, stores are created as `new Store(data)` rather than with a factory function.
72
74
 
73
75
  ## Single store or multiple stores
@@ -103,7 +105,7 @@ let ItemCard = ({ id }) => {
103
105
 
104
106
  While `useStore(itemStore)` in this component would trigger a re-render in response to any changes in the `itemStore` (which can be fine with a small store), with `useStore(itemStore, shouldUpdate)` the `ItemCard` component has a more targeted subscription to the store: in this example, a re-render will only be triggered if the `revision` property of the item with the given `id` has changed.
105
107
 
106
- ## Providing shared state
108
+ ## Sharing state via Context
107
109
 
108
110
  Shared state can be provided to the app by means of a regular React Context provider:
109
111
 
@@ -177,12 +179,14 @@ A standalone store initialized outside a component can be used by the component
177
179
 
178
180
  ## Persistence across page reloads
179
181
 
182
+ Replacing `new Store(data)` with `new PersistentStore(data, storageKey)` as shown below gets the store's state value initially restored from and saved whenever updated to `storageKey` in `localStorage`. (Pass `{ session: true }` as the `options` parameter of `new PersistentStore(data, storageKey, options?)` to use `sessionStorage` instead of `localStorage`.) Otherwise, persistent stores work pretty much like regular stores described above.
183
+
180
184
  ```js
181
185
  import { PersistentStore } from "@t8/react-store";
182
186
 
183
187
  let counterStore = new PersistentStore(0, "counter");
184
188
  ```
185
189
 
186
- The store's state value is initially restored from and saved whenever updated to the `"counter"` key of `localStorage`. (Pass `{ session: true }` as the `options` parameter of `new PersistentStore(data, storageKey, options?)` to use `sessionStorage` instead of `localStorage`.) Otherwise, `counterStore` works pretty much like a regular store described above.
190
+ 🔹 The way data gets saved to and restored from a browser storage entry (including filtering out certain data or otherwise rearranging the saved data) can be redefined by setting `options.serialize` and `options.deserialize` in `new PersistentStore(data, storageKey, options?)`. By default, these options act like `JSON.stringify()` and `JSON.parse()` respectively.
187
191
 
188
- The way data gets saved to and restored from a browser storage entry (including filtering out certain data or otherwise rearranging the saved data) can be overridden by setting `options.serialize` and `options.deserialize` in `new PersistentStore(data, storageKey, options?)`. By default, they are `JSON.stringify()` and `JSON.parse()`.
192
+ 🔹 `PersistentStore` skips interaction with the browser storage in non-browser environments, which makes it equally usable with SSR.
package/dist/index.js CHANGED
@@ -121,7 +121,8 @@ var PersistentStore = class extends Store {
121
121
  // src/useStore.ts
122
122
  import { useEffect, useMemo, useRef, useState } from "react";
123
123
  function useStore(store, shouldUpdate = true) {
124
- if (!isStore(store)) throw new Error("'store' is not an instance of Store");
124
+ if (!isStore(store))
125
+ throw new Error("'store' is not an instance of Store");
125
126
  let [, setRevision] = useState(-1);
126
127
  let state = store.getState();
127
128
  let setState = useMemo(() => store.setState.bind(store), [store]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@t8/react-store",
3
- "version": "1.2.1",
3
+ "version": "1.2.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",
package/src/useStore.ts CHANGED
@@ -36,7 +36,8 @@ export function useStore<T>(
36
36
  store: Store<T>,
37
37
  shouldUpdate: ShouldUpdate<T> = true,
38
38
  ): [T, SetStoreState<T>] {
39
- if (!isStore(store)) throw new Error("'store' is not an instance of Store");
39
+ if (!isStore<T>(store))
40
+ throw new Error("'store' is not an instance of Store");
40
41
 
41
42
  let [, setRevision] = useState(-1);
42
43