zustand-mutable 0.1.0 → 0.1.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/LICENSE +21 -0
- package/README.md +220 -0
- package/package.json +14 -11
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Danilo Britto
|
|
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,220 @@
|
|
|
1
|
+
# zustand-mutable
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/zustand-mutable)
|
|
4
|
+
[](https://github.com/zustandjs/zustand-mutable/actions/workflows/test.yml)
|
|
5
|
+
[](https://github.com/zustandjs/zustand-mutable/blob/main/LICENSE)
|
|
6
|
+
|
|
7
|
+
A sweet way to use immer-like mutable updates with Zustand.
|
|
8
|
+
|
|
9
|
+
## Introduction
|
|
10
|
+
|
|
11
|
+
Zustand's immutable state updates can become verbose when dealing with deeply nested state. This middleware lets you write state updates using a mutable API pattern, similar to Immer's draft pattern.
|
|
12
|
+
|
|
13
|
+
**Key benefit:** You choose your own produce function - use [Immer](https://github.com/immerjs/immer), [Mutative](https://github.com/unadlib/mutative), [Limu](https://github.com/tnfe/limu), or any library that follows the produce pattern.
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install zustand-mutable zustand
|
|
19
|
+
|
|
20
|
+
# Plus your preferred produce library (pick one):
|
|
21
|
+
npm install immer # Most popular
|
|
22
|
+
npm install mutative # Faster alternative
|
|
23
|
+
npm install limu # Another option
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Quick Start
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
import { create } from 'zustand'
|
|
30
|
+
import { mutable } from 'zustand-mutable'
|
|
31
|
+
import { produce } from 'immer'
|
|
32
|
+
|
|
33
|
+
type CounterState = {
|
|
34
|
+
count: number
|
|
35
|
+
inc: () => void
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const useStore = create<CounterState>()(
|
|
39
|
+
mutable(
|
|
40
|
+
(set, get) => ({
|
|
41
|
+
count: 0,
|
|
42
|
+
inc: () =>
|
|
43
|
+
set((state) => {
|
|
44
|
+
state.count = get().count + 1 // Mutate directly!
|
|
45
|
+
}),
|
|
46
|
+
}),
|
|
47
|
+
produce,
|
|
48
|
+
),
|
|
49
|
+
)
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## API Reference
|
|
53
|
+
|
|
54
|
+
### `mutable(initializer, produceFn)`
|
|
55
|
+
|
|
56
|
+
Wraps your Zustand store initializer to enable mutable-style updates.
|
|
57
|
+
|
|
58
|
+
| Parameter | Type | Description |
|
|
59
|
+
| ------------- | ------------------------------------------------- | ------------------------------------------- |
|
|
60
|
+
| `initializer` | `StateCreator<T>` | Your standard Zustand store initializer |
|
|
61
|
+
| `produceFn` | `(recipe: (state: T) => void) => (state: T) => T` | A produce function from your chosen library |
|
|
62
|
+
|
|
63
|
+
## Supported Libraries
|
|
64
|
+
|
|
65
|
+
### Immer
|
|
66
|
+
|
|
67
|
+
The most popular immutable state library.
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
import { produce } from 'immer'
|
|
71
|
+
|
|
72
|
+
const useStore = create<State>()(
|
|
73
|
+
mutable(
|
|
74
|
+
(set, get) => ({
|
|
75
|
+
// your state and actions
|
|
76
|
+
}),
|
|
77
|
+
produce,
|
|
78
|
+
),
|
|
79
|
+
)
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Mutative
|
|
83
|
+
|
|
84
|
+
A faster alternative to Immer with similar API.
|
|
85
|
+
|
|
86
|
+
```typescript
|
|
87
|
+
import { create as mutativeProduce } from 'mutative'
|
|
88
|
+
|
|
89
|
+
const useStore = create<State>()(
|
|
90
|
+
mutable(
|
|
91
|
+
(set, get) => ({
|
|
92
|
+
// your state and actions
|
|
93
|
+
}),
|
|
94
|
+
mutativeProduce,
|
|
95
|
+
),
|
|
96
|
+
)
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### Limu
|
|
100
|
+
|
|
101
|
+
Another immutable update library. Requires a wrapper function.
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
import { produce } from 'limu'
|
|
105
|
+
|
|
106
|
+
const useStore = create<State>()(
|
|
107
|
+
mutable(
|
|
108
|
+
(set, get) => ({
|
|
109
|
+
// your state and actions
|
|
110
|
+
}),
|
|
111
|
+
(recipe) => produce(recipe),
|
|
112
|
+
),
|
|
113
|
+
)
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## Middleware Composition
|
|
117
|
+
|
|
118
|
+
`zustand-mutable` works seamlessly with other Zustand middleware.
|
|
119
|
+
|
|
120
|
+
### With devtools
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
import { devtools } from 'zustand/middleware'
|
|
124
|
+
|
|
125
|
+
const useStore = create<CounterState>()(
|
|
126
|
+
mutable(
|
|
127
|
+
devtools(
|
|
128
|
+
(set, get) => ({
|
|
129
|
+
count: 0,
|
|
130
|
+
inc: () =>
|
|
131
|
+
set(
|
|
132
|
+
(state) => {
|
|
133
|
+
state.count = get().count + 1
|
|
134
|
+
},
|
|
135
|
+
false,
|
|
136
|
+
{ type: 'inc', by: 1 },
|
|
137
|
+
),
|
|
138
|
+
}),
|
|
139
|
+
{ name: 'counter' },
|
|
140
|
+
),
|
|
141
|
+
produce,
|
|
142
|
+
),
|
|
143
|
+
)
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### With persist
|
|
147
|
+
|
|
148
|
+
```typescript
|
|
149
|
+
import { persist } from 'zustand/middleware'
|
|
150
|
+
|
|
151
|
+
const useStore = create<CounterState>()(
|
|
152
|
+
persist(
|
|
153
|
+
mutable(
|
|
154
|
+
(set, get) => ({
|
|
155
|
+
count: 0,
|
|
156
|
+
inc: () =>
|
|
157
|
+
set((state) => {
|
|
158
|
+
state.count = get().count + 1
|
|
159
|
+
}),
|
|
160
|
+
}),
|
|
161
|
+
produce,
|
|
162
|
+
),
|
|
163
|
+
{ name: 'counter-storage' },
|
|
164
|
+
),
|
|
165
|
+
)
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### Combining Multiple Middleware
|
|
169
|
+
|
|
170
|
+
You can stack multiple middleware together:
|
|
171
|
+
|
|
172
|
+
```typescript
|
|
173
|
+
import { devtools, persist, subscribeWithSelector } from 'zustand/middleware'
|
|
174
|
+
|
|
175
|
+
const useStore = create<CounterState>()(
|
|
176
|
+
devtools(
|
|
177
|
+
subscribeWithSelector(
|
|
178
|
+
persist(
|
|
179
|
+
mutable(
|
|
180
|
+
(set, get) => ({
|
|
181
|
+
count: 0,
|
|
182
|
+
inc: () =>
|
|
183
|
+
set((state) => {
|
|
184
|
+
state.count = get().count + 1
|
|
185
|
+
}),
|
|
186
|
+
}),
|
|
187
|
+
produce,
|
|
188
|
+
),
|
|
189
|
+
{ name: 'counter' },
|
|
190
|
+
),
|
|
191
|
+
),
|
|
192
|
+
{ name: 'counter-devtools' },
|
|
193
|
+
),
|
|
194
|
+
)
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
## TypeScript
|
|
198
|
+
|
|
199
|
+
This library is written in TypeScript and provides full type safety out of the box.
|
|
200
|
+
|
|
201
|
+
- The `Draft<T>` type automatically makes your state mutable inside updater functions
|
|
202
|
+
- Proper type inference is maintained through middleware composition
|
|
203
|
+
- Works with strict TypeScript configurations
|
|
204
|
+
|
|
205
|
+
```typescript
|
|
206
|
+
set((state) => {
|
|
207
|
+
// `state` is typed as Draft<YourState>
|
|
208
|
+
// You can mutate it directly with full type safety
|
|
209
|
+
state.nested.deeply.value = 'new value'
|
|
210
|
+
})
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
## Requirements
|
|
214
|
+
|
|
215
|
+
- `zustand` >= 5.0.9 (peer dependency)
|
|
216
|
+
- One of: `immer`, `mutative`, or `limu`
|
|
217
|
+
|
|
218
|
+
## License
|
|
219
|
+
|
|
220
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zustand-mutable",
|
|
3
|
+
"version": "0.1.1",
|
|
3
4
|
"private": false,
|
|
4
5
|
"description": "A sweet way to use immer-like mutable updates",
|
|
5
|
-
"version": "0.1.0",
|
|
6
6
|
"author": "Danilo Britto",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
9
9
|
"url": "git+https://github.com/zustandjs/zustand-mutable.git"
|
|
10
10
|
},
|
|
11
|
-
"type": "module",
|
|
12
11
|
"source": "./src/index.ts",
|
|
12
|
+
"files": [
|
|
13
|
+
"dist"
|
|
14
|
+
],
|
|
15
|
+
"type": "module",
|
|
16
|
+
"sideEffects": false,
|
|
13
17
|
"main": "./dist/index.umd.cjs",
|
|
14
18
|
"module": "./dist/index.js",
|
|
15
19
|
"exports": {
|
|
@@ -18,12 +22,10 @@
|
|
|
18
22
|
"require": "./dist/index.umd.cjs"
|
|
19
23
|
}
|
|
20
24
|
},
|
|
21
|
-
"
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
],
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
25
28
|
"devDependencies": {
|
|
26
|
-
"@biomejs/biome": "2.3.10",
|
|
27
29
|
"@ossjs/release": "^0.10.1",
|
|
28
30
|
"@testing-library/jest-dom": "^6.9.1",
|
|
29
31
|
"@testing-library/react": "^16.3.1",
|
|
@@ -33,6 +35,9 @@
|
|
|
33
35
|
"immer": "^11.1.3",
|
|
34
36
|
"limu": "^4.1.1",
|
|
35
37
|
"mutative": "^1.3.0",
|
|
38
|
+
"oxfmt": "^0.21.0",
|
|
39
|
+
"oxlint": "^1.36.0",
|
|
40
|
+
"prettier": "3.7.4",
|
|
36
41
|
"react": "^19.2.3",
|
|
37
42
|
"typescript": "~5.9.3",
|
|
38
43
|
"vite": "npm:rolldown-vite@7.2.5",
|
|
@@ -43,14 +48,12 @@
|
|
|
43
48
|
"peerDependencies": {
|
|
44
49
|
"zustand": "^5.0.9"
|
|
45
50
|
},
|
|
46
|
-
"publishConfig": {
|
|
47
|
-
"access": "public"
|
|
48
|
-
},
|
|
49
51
|
"scripts": {
|
|
50
52
|
"build": "vite build",
|
|
51
53
|
"test": "pnpm run \"/^test:.*/\"",
|
|
52
54
|
"test:types": "tsc --noEmit",
|
|
53
|
-
"test:
|
|
55
|
+
"test:lint": "oxlint",
|
|
56
|
+
"test:fmt": "oxfmt --check",
|
|
54
57
|
"test:spec": "vitest run",
|
|
55
58
|
"release": "release publish"
|
|
56
59
|
}
|