valtio-define 0.2.0 → 0.3.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/README.md +12 -16
- package/dist/index.mjs +2 -2
- package/package.json +7 -3
package/README.md
CHANGED
|
@@ -31,11 +31,11 @@ const store = defineStore({
|
|
|
31
31
|
})
|
|
32
32
|
|
|
33
33
|
function Counter() {
|
|
34
|
-
const
|
|
34
|
+
const { count, increment } = useStore(store)
|
|
35
35
|
return (
|
|
36
36
|
<div>
|
|
37
|
-
<button onClick={
|
|
38
|
-
<div>{
|
|
37
|
+
<button onClick={increment}>Increment</button>
|
|
38
|
+
<div>{count}</div>
|
|
39
39
|
</div>
|
|
40
40
|
)
|
|
41
41
|
}
|
|
@@ -119,19 +119,15 @@ function DataComponent() {
|
|
|
119
119
|
### Persistence
|
|
120
120
|
|
|
121
121
|
```tsx
|
|
122
|
-
const store = defineStore(
|
|
123
|
-
{
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
this.count++
|
|
128
|
-
},
|
|
122
|
+
const store = defineStore({
|
|
123
|
+
state: () => ({ count: 0 }),
|
|
124
|
+
actions: {
|
|
125
|
+
increment() {
|
|
126
|
+
this.count++
|
|
129
127
|
},
|
|
130
128
|
},
|
|
131
|
-
{
|
|
132
|
-
|
|
133
|
-
}
|
|
134
|
-
)
|
|
129
|
+
persist: true // or { key: 'my-store', storage: localStorage, paths: ['count'] }
|
|
130
|
+
})
|
|
135
131
|
```
|
|
136
132
|
|
|
137
133
|
If the persist is a boolean value, it will use `structure-id` to generate a unique key for the store.
|
|
@@ -193,7 +189,7 @@ function App() {
|
|
|
193
189
|
|
|
194
190
|
## API
|
|
195
191
|
|
|
196
|
-
### `defineStore(store
|
|
192
|
+
### `defineStore(store)`
|
|
197
193
|
|
|
198
194
|
Creates a store with state, actions, and getters.
|
|
199
195
|
|
|
@@ -201,7 +197,7 @@ Creates a store with state, actions, and getters.
|
|
|
201
197
|
- `store.state`: Initial state object or factory function
|
|
202
198
|
- `store.actions`: Object containing action methods
|
|
203
199
|
- `store.getters`: Object containing getter methods
|
|
204
|
-
- `
|
|
200
|
+
- `store.persist`: Persistence configuration (boolean or object)
|
|
205
201
|
|
|
206
202
|
**Returns:** Store instance with reactive state and actions
|
|
207
203
|
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createElement } from "react";
|
|
2
2
|
import { proxy, ref, subscribe, useSnapshot } from "valtio";
|
|
3
|
-
import {
|
|
3
|
+
import { tryParseJson } from "@hairy/utils";
|
|
4
4
|
import { generateStructureId } from "structure-id";
|
|
5
5
|
|
|
6
6
|
//#region src/utils/index.ts
|
|
@@ -48,7 +48,7 @@ function set(obj, path, value) {
|
|
|
48
48
|
function proxyWithPersistent(initialObject, options = {}) {
|
|
49
49
|
options.key = options.key || generateStructureId(initialObject);
|
|
50
50
|
const storage = options.storage || (typeof localStorage !== "undefined" ? localStorage : void 0);
|
|
51
|
-
const state = proxy(
|
|
51
|
+
const state = proxy(tryParseJson(storage?.getItem(options.key)) || initialObject);
|
|
52
52
|
subscribe(state, () => {
|
|
53
53
|
const paths = options.paths || Object.keys(state);
|
|
54
54
|
const statePaths = {};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "valtio-define",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.3.0",
|
|
5
5
|
"description": "⚡quickly create a fully functional and robust Valtio factory",
|
|
6
6
|
"author": "Hairyf <wwu710632@gmail.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"react": "^18.2.0"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@hairy/utils": "^1.
|
|
32
|
+
"@hairy/utils": "^1.46.0",
|
|
33
33
|
"structure-id": "^1.2.9",
|
|
34
34
|
"valtio": "^2.2.0"
|
|
35
35
|
},
|
|
@@ -40,9 +40,12 @@
|
|
|
40
40
|
"@types/node": "^24.10.0",
|
|
41
41
|
"@types/react": "^19.2.6",
|
|
42
42
|
"@vitejs/plugin-react": "^5.1.1",
|
|
43
|
+
"@vitest/browser-playwright": "^4.0.15",
|
|
44
|
+
"@vitest/coverage-v8": "^4.0.15",
|
|
43
45
|
"bumpp": "^10.3.1",
|
|
44
46
|
"eslint": "^9.39.1",
|
|
45
47
|
"lint-staged": "^16.2.6",
|
|
48
|
+
"playwright": "^1.57.0",
|
|
46
49
|
"react": "^19.2.0",
|
|
47
50
|
"simple-git-hooks": "^2.13.1",
|
|
48
51
|
"tinyexec": "^1.0.2",
|
|
@@ -50,7 +53,8 @@
|
|
|
50
53
|
"tsx": "^4.20.6",
|
|
51
54
|
"typescript": "^5.9.3",
|
|
52
55
|
"vite": "^7.2.1",
|
|
53
|
-
"vitest": "^4.0.
|
|
56
|
+
"vitest": "^4.0.15",
|
|
57
|
+
"vitest-browser-react": "^2.0.2",
|
|
54
58
|
"vitest-package-exports": "^0.1.1",
|
|
55
59
|
"yaml": "^2.8.1"
|
|
56
60
|
},
|