@lite-fsm/react 2.0.0 → 2.0.1
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 +87 -0
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# @lite-fsm/react
|
|
2
|
+
|
|
3
|
+
React bindings for `lite-fsm`: a context provider, selector and transition hooks, hydration helpers, and a standalone `defineMachine` hook API.
|
|
4
|
+
|
|
5
|
+
The package is marked with `"use client"`. It can be imported from SSR/RSC code, but hooks and providers must run in the client React tree.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @lite-fsm/core @lite-fsm/react
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
For mutable Immer-style reducers, add:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @lite-fsm/middleware immer
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Quick Example
|
|
20
|
+
|
|
21
|
+
```tsx
|
|
22
|
+
import { MachineManager, createMachine, type FSMEvent } from "@lite-fsm/core";
|
|
23
|
+
import { FSMContextProvider, useSelector, useTransition } from "@lite-fsm/react";
|
|
24
|
+
|
|
25
|
+
type CounterEvent = FSMEvent<"INCREMENT"> | FSMEvent<"DECREMENT"> | FSMEvent<"RESET">;
|
|
26
|
+
|
|
27
|
+
const counter = createMachine<CounterEvent>({
|
|
28
|
+
config: {
|
|
29
|
+
IDLE: {
|
|
30
|
+
INCREMENT: null,
|
|
31
|
+
DECREMENT: null,
|
|
32
|
+
RESET: null,
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
initialState: "IDLE",
|
|
36
|
+
initialContext: { count: 0 },
|
|
37
|
+
reducer: (slice, event) => {
|
|
38
|
+
switch (event.type) {
|
|
39
|
+
case "INCREMENT":
|
|
40
|
+
return { ...slice, context: { count: slice.context.count + 1 } };
|
|
41
|
+
case "DECREMENT":
|
|
42
|
+
return { ...slice, context: { count: slice.context.count - 1 } };
|
|
43
|
+
case "RESET":
|
|
44
|
+
return { ...slice, context: { count: 0 } };
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
const manager = MachineManager({ counter });
|
|
50
|
+
|
|
51
|
+
export function App() {
|
|
52
|
+
return (
|
|
53
|
+
<FSMContextProvider machineManager={manager}>
|
|
54
|
+
<Counter />
|
|
55
|
+
</FSMContextProvider>
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function Counter() {
|
|
60
|
+
const count = useSelector((state) => state.counter.context.count);
|
|
61
|
+
const transition = useTransition<CounterEvent>();
|
|
62
|
+
|
|
63
|
+
return (
|
|
64
|
+
<div>
|
|
65
|
+
<p>Count: {count}</p>
|
|
66
|
+
<button onClick={() => transition({ type: "INCREMENT" })}>+</button>
|
|
67
|
+
<button onClick={() => transition({ type: "DECREMENT" })}>-</button>
|
|
68
|
+
<button onClick={() => transition({ type: "RESET" })}>Reset</button>
|
|
69
|
+
</div>
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Main Exports
|
|
75
|
+
|
|
76
|
+
- `FSMContextProvider` - provides a `MachineManager` to React hooks.
|
|
77
|
+
- `useSelector` - subscribes to a selected part of manager state.
|
|
78
|
+
- `useTransition` - returns `manager.transition`.
|
|
79
|
+
- `useManager` - returns the current manager from context.
|
|
80
|
+
- `FSMHydrationBoundary` and `useHydrateSnapshot` - React hydration helpers for snapshots.
|
|
81
|
+
- `defineMachine` - creates a standalone shared machine hook.
|
|
82
|
+
|
|
83
|
+
## Documentation
|
|
84
|
+
|
|
85
|
+
- [Full documentation](https://alexandergureev.github.io/lite-fsm/)
|
|
86
|
+
- [React package guide](https://alexandergureev.github.io/lite-fsm/packages/react)
|
|
87
|
+
- [React API reference](https://alexandergureev.github.io/lite-fsm/api/react)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lite-fsm/react",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "React bindings for lite-fsm",
|
|
6
6
|
"license": "MIT",
|
|
@@ -56,7 +56,7 @@
|
|
|
56
56
|
},
|
|
57
57
|
"dependencies": {
|
|
58
58
|
"use-sync-external-store": ">=1.2.0",
|
|
59
|
-
"@lite-fsm/core": "2.0.
|
|
59
|
+
"@lite-fsm/core": "2.0.1"
|
|
60
60
|
},
|
|
61
61
|
"peerDependencies": {
|
|
62
62
|
"react": ">=18.0.0"
|