react-mirrorstate 0.1.0-alpha.0 → 0.1.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 +81 -0
  2. package/package.json +2 -2
package/README.md ADDED
@@ -0,0 +1,81 @@
1
+ # `react-mirrorstate`
2
+
3
+ React hook for bi-directional state synchronization in JSON on disk.
4
+
5
+ Requires Vite and `vite-plugin-mirrorstate` plugin.
6
+
7
+ Changes made in the UI are reflected on disk, and changes on disk propagate to the UI.
8
+
9
+ ## Installation
10
+
11
+ ```bash
12
+ npm install react-mirrorstate vite-plugin-mirrorstate
13
+ ```
14
+
15
+ ## Example
16
+
17
+ Create a `state.mirror.json` file:
18
+
19
+ ```json
20
+ {
21
+ "count": 20,
22
+ "message": "Hello World"
23
+ }
24
+ ```
25
+
26
+ Start `vite` with `vite-plugin-mirrorstate` enabled:
27
+
28
+ ```typescript
29
+ import { defineConfig } from "vite";
30
+ import react from "@vitejs/plugin-react";
31
+ import mirrorstate from "vite-plugin-mirrorstate";
32
+
33
+ export default defineConfig({
34
+ plugins: [react(), mirrorstate()],
35
+ });
36
+ ```
37
+
38
+ Set up a React component:
39
+
40
+ ```tsx
41
+ import { useMirrorState } from "react-mirrorstate";
42
+
43
+ function App() {
44
+ const [state, updateState] = useMirrorState("state", {
45
+ count: 0,
46
+ message: "",
47
+ });
48
+
49
+ return (
50
+ <div>
51
+ <p>
52
+ {state.message}: {state.count}
53
+ </p>
54
+ <button
55
+ onClick={() =>
56
+ updateState((draft) => {
57
+ draft.count++;
58
+ })
59
+ }
60
+ >
61
+ Increment
62
+ </button>
63
+ </div>
64
+ );
65
+ }
66
+ ```
67
+
68
+ ## API: `useMirrorState([key], [initialState])`
69
+
70
+ - `key`: String identifier for the state file (without `.mirror.json` extension)
71
+ - `initialState`: Default state if no persisted state exists
72
+
73
+ Returns `[state, updateState]` similar to React's `useState`, but:
74
+
75
+ - `state` is synchronized with `[key].mirror.json` file
76
+ - `updateState` accepts an Immer draft function for mutations
77
+
78
+ ## License
79
+
80
+ MIT
81
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-mirrorstate",
3
- "version": "0.1.0-alpha.0",
3
+ "version": "0.1.0",
4
4
  "description": "React library for bidirectional state synchronization with MirrorState",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -27,7 +27,7 @@
27
27
  "typescript": "^5.3.3"
28
28
  },
29
29
  "peerDependencies": {
30
- "react": "^18.0.0"
30
+ "react": ">=18.0.0"
31
31
  },
32
32
  "files": [
33
33
  "dist"