@better-state/react 0.1.0 → 0.2.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.
- package/LICENSE +21 -0
- package/README.md +195 -0
- package/package.json +29 -11
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Better-State Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
# @better-state/react
|
|
2
|
+
|
|
3
|
+
React hooks for [Better-State](https://github.com/Nathanim1919/better-state). Real-time synced state with one line of code.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @better-state/client @better-state/react
|
|
9
|
+
# or
|
|
10
|
+
pnpm add @better-state/client @better-state/react
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Requires React 18+ and a running [`@better-state/server`](https://www.npmjs.com/package/@better-state/server).
|
|
14
|
+
|
|
15
|
+
## Quick Start
|
|
16
|
+
|
|
17
|
+
```tsx
|
|
18
|
+
import { BetterStateProvider, useBetterState } from '@better-state/react'
|
|
19
|
+
|
|
20
|
+
function App() {
|
|
21
|
+
return (
|
|
22
|
+
<BetterStateProvider
|
|
23
|
+
url="http://localhost:3001"
|
|
24
|
+
options={{ apiKey: 'your-api-key' }}
|
|
25
|
+
>
|
|
26
|
+
<Counter />
|
|
27
|
+
</BetterStateProvider>
|
|
28
|
+
)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function Counter() {
|
|
32
|
+
const [count, setCount, updateCount] = useBetterState('counter', 0)
|
|
33
|
+
|
|
34
|
+
return (
|
|
35
|
+
<div>
|
|
36
|
+
<h1>{count}</h1>
|
|
37
|
+
<button onClick={() => updateCount(n => n + 1)}>+1</button>
|
|
38
|
+
<button onClick={() => updateCount(n => n - 1)}>-1</button>
|
|
39
|
+
<button onClick={() => setCount(0)}>Reset</button>
|
|
40
|
+
</div>
|
|
41
|
+
)
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Open the same page in two tabs. The counter syncs in real-time.
|
|
46
|
+
|
|
47
|
+
## API
|
|
48
|
+
|
|
49
|
+
### `<BetterStateProvider>`
|
|
50
|
+
|
|
51
|
+
Wraps your app and creates a single Better-State client for the component tree.
|
|
52
|
+
|
|
53
|
+
```tsx
|
|
54
|
+
<BetterStateProvider
|
|
55
|
+
url="http://localhost:3001"
|
|
56
|
+
options={{
|
|
57
|
+
apiKey: string, // required
|
|
58
|
+
namespace?: string, // default: 'default'
|
|
59
|
+
debug?: boolean, // default: false
|
|
60
|
+
}}
|
|
61
|
+
>
|
|
62
|
+
{children}
|
|
63
|
+
</BetterStateProvider>
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Place it once at the root of your app (or per-feature if you need multiple servers).
|
|
67
|
+
|
|
68
|
+
### `useBetterState(key, initialValue)`
|
|
69
|
+
|
|
70
|
+
Synced state hook. Works like `useState`, but the value is shared across all connected clients.
|
|
71
|
+
|
|
72
|
+
```tsx
|
|
73
|
+
const [value, set, update] = useBetterState('my-key', initialValue)
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
| Return | Type | Description |
|
|
77
|
+
|--------|------|-------------|
|
|
78
|
+
| `value` | `T` | Current value (reactive) |
|
|
79
|
+
| `set` | `(value: T) => void` | Replace the value |
|
|
80
|
+
| `update` | `(fn: (prev: T) => T) => void` | Transform the value |
|
|
81
|
+
|
|
82
|
+
Updates are **optimistic** — the UI updates instantly, then syncs to the server in the background.
|
|
83
|
+
|
|
84
|
+
```tsx
|
|
85
|
+
function TodoList() {
|
|
86
|
+
const [todos, setTodos, updateTodos] = useBetterState('todos', [])
|
|
87
|
+
|
|
88
|
+
const addTodo = (text: string) => {
|
|
89
|
+
updateTodos(list => [...list, { id: crypto.randomUUID(), text, done: false }])
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const toggleTodo = (id: string) => {
|
|
93
|
+
updateTodos(list =>
|
|
94
|
+
list.map(t => t.id === id ? { ...t, done: !t.done } : t)
|
|
95
|
+
)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return (
|
|
99
|
+
<ul>
|
|
100
|
+
{todos.map(t => (
|
|
101
|
+
<li key={t.id} onClick={() => toggleTodo(t.id)}>
|
|
102
|
+
{t.done ? '✓' : '○'} {t.text}
|
|
103
|
+
</li>
|
|
104
|
+
))}
|
|
105
|
+
</ul>
|
|
106
|
+
)
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### `useConnectionStatus()`
|
|
111
|
+
|
|
112
|
+
Returns the current connection status. Re-renders when it changes.
|
|
113
|
+
|
|
114
|
+
```tsx
|
|
115
|
+
import { useConnectionStatus } from '@better-state/react'
|
|
116
|
+
|
|
117
|
+
function StatusIndicator() {
|
|
118
|
+
const status = useConnectionStatus()
|
|
119
|
+
|
|
120
|
+
return (
|
|
121
|
+
<span>
|
|
122
|
+
{status === 'connected' && 'Online'}
|
|
123
|
+
{status === 'connecting' && 'Connecting...'}
|
|
124
|
+
{status === 'disconnected' && 'Offline'}
|
|
125
|
+
</span>
|
|
126
|
+
)
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
Returns: `'connecting' | 'connected' | 'disconnected'`
|
|
131
|
+
|
|
132
|
+
### `useBetterStateClient()`
|
|
133
|
+
|
|
134
|
+
Returns the underlying `BetterStateClient` instance for advanced use cases (disconnect, error handling, conflict listeners).
|
|
135
|
+
|
|
136
|
+
```tsx
|
|
137
|
+
import { useBetterStateClient } from '@better-state/react'
|
|
138
|
+
|
|
139
|
+
function Settings() {
|
|
140
|
+
const client = useBetterStateClient()
|
|
141
|
+
|
|
142
|
+
useEffect(() => {
|
|
143
|
+
const unsub = client.onError(err => {
|
|
144
|
+
console.error(err.code, err.message)
|
|
145
|
+
})
|
|
146
|
+
return unsub
|
|
147
|
+
}, [client])
|
|
148
|
+
|
|
149
|
+
return <button onClick={() => client.disconnect()}>Disconnect</button>
|
|
150
|
+
}
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## Full Example
|
|
154
|
+
|
|
155
|
+
```tsx
|
|
156
|
+
import { BetterStateProvider, useBetterState, useConnectionStatus } from '@better-state/react'
|
|
157
|
+
|
|
158
|
+
function App() {
|
|
159
|
+
return (
|
|
160
|
+
<BetterStateProvider
|
|
161
|
+
url="http://localhost:3001"
|
|
162
|
+
options={{ apiKey: 'your-api-key' }}
|
|
163
|
+
>
|
|
164
|
+
<StatusBar />
|
|
165
|
+
<Counter />
|
|
166
|
+
</BetterStateProvider>
|
|
167
|
+
)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function StatusBar() {
|
|
171
|
+
const status = useConnectionStatus()
|
|
172
|
+
return <div>{status === 'connected' ? 'Online' : 'Offline'}</div>
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function Counter() {
|
|
176
|
+
const [count, , updateCount] = useBetterState('counter', 0)
|
|
177
|
+
return (
|
|
178
|
+
<div>
|
|
179
|
+
<h1>{count}</h1>
|
|
180
|
+
<button onClick={() => updateCount(n => n + 1)}>+1</button>
|
|
181
|
+
</div>
|
|
182
|
+
)
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export default App
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
## Peer Dependencies
|
|
189
|
+
|
|
190
|
+
- `react` >= 18.0.0
|
|
191
|
+
- `@better-state/client` >= 0.1.0
|
|
192
|
+
|
|
193
|
+
## License
|
|
194
|
+
|
|
195
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@better-state/react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "React hooks for Better-State — real-time synced state with one line of code",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -11,8 +11,18 @@
|
|
|
11
11
|
"types": "./dist/index.d.ts"
|
|
12
12
|
}
|
|
13
13
|
},
|
|
14
|
-
"files": [
|
|
15
|
-
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"keywords": [
|
|
18
|
+
"react",
|
|
19
|
+
"hooks",
|
|
20
|
+
"state",
|
|
21
|
+
"sync",
|
|
22
|
+
"realtime",
|
|
23
|
+
"websocket",
|
|
24
|
+
"better-state"
|
|
25
|
+
],
|
|
16
26
|
"license": "MIT",
|
|
17
27
|
"repository": {
|
|
18
28
|
"type": "git",
|
|
@@ -22,19 +32,27 @@
|
|
|
22
32
|
"publishConfig": {
|
|
23
33
|
"access": "public"
|
|
24
34
|
},
|
|
25
|
-
"scripts": {
|
|
26
|
-
"build": "tsc",
|
|
27
|
-
"dev": "tsc --watch",
|
|
28
|
-
"clean": "rm -rf dist"
|
|
29
|
-
},
|
|
30
35
|
"peerDependencies": {
|
|
31
36
|
"@better-state/client": ">=0.1.0",
|
|
32
37
|
"react": ">=18.0.0"
|
|
33
38
|
},
|
|
34
39
|
"devDependencies": {
|
|
35
|
-
"@better-state/
|
|
40
|
+
"@better-state/server": "^0.3.1",
|
|
41
|
+
"@testing-library/jest-dom": "^6.9.1",
|
|
42
|
+
"@testing-library/react": "^16.3.2",
|
|
36
43
|
"@types/react": "^19.0.0",
|
|
44
|
+
"@types/react-dom": "^19.2.3",
|
|
45
|
+
"happy-dom": "^20.7.0",
|
|
37
46
|
"react": "^19.0.0",
|
|
38
|
-
"
|
|
47
|
+
"react-dom": "^19.2.4",
|
|
48
|
+
"typescript": "^5.7.0",
|
|
49
|
+
"vitest": "^4.0.18",
|
|
50
|
+
"@better-state/client": "0.2.0"
|
|
51
|
+
},
|
|
52
|
+
"scripts": {
|
|
53
|
+
"build": "tsc",
|
|
54
|
+
"dev": "tsc --watch",
|
|
55
|
+
"test": "vitest run",
|
|
56
|
+
"clean": "rm -rf dist"
|
|
39
57
|
}
|
|
40
|
-
}
|
|
58
|
+
}
|