define-zustand 3.0.3 → 3.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.
package/README.md ADDED
@@ -0,0 +1,66 @@
1
+ # define-zustand
2
+
3
+ > Quickly define [zustand](https://github.com/pmndrs/zustand) state
4
+
5
+ ## Install
6
+
7
+ ```shell
8
+ pnpm i define-zustand
9
+ npm i define-zustand
10
+ ```
11
+
12
+ ## Use
13
+
14
+ ```tsx
15
+ import { defineStore, defineContext, defineStateFactory } from 'define-zustand'
16
+
17
+ const stateFatory = defineStateFactory({
18
+ state: () => ({
19
+ a: 1,
20
+ b: 1
21
+ }),
22
+ action: () => ({}),
23
+ getter: {
24
+ count: state => state.a + state.b
25
+ }
26
+ })
27
+ /**
28
+ * @return zustand hooks
29
+ */
30
+ const useStore = defineStore(stateFatory())
31
+ const { Provider, useContext } = defineContext(stateFatory())
32
+
33
+ function Child() {
34
+ const count = useContext(state => state.count)
35
+ return <div>{count}</div>
36
+ }
37
+ function ReactComponent() {
38
+ const count = useStore(state => state.count)
39
+ const setState = useStore(state => state.setState)
40
+ const reset = useStore(state => state.reset)
41
+
42
+ useEffect(() => {
43
+ return () => {
44
+ reset()
45
+ }
46
+ }, [])
47
+
48
+ return (
49
+ <Provider>
50
+ <div>
51
+ <button onClick={() => setState({ a: 2 })}>setA</button>
52
+ <button
53
+ onClick={() =>
54
+ setState(state => {
55
+ state.b += 1
56
+ })
57
+ }>
58
+ setB
59
+ </button>
60
+ <div>{count}</div>
61
+ </div>
62
+ <Child />
63
+ </Provider>
64
+ )
65
+ }
66
+ ```
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- export { creator, defineContext, defineStore } from './utils.js';
1
+ export { creator, defineContext, defineStateFactory, defineStore } from './utils.js';
package/dist/utils.d.ts CHANGED
@@ -1,11 +1,12 @@
1
1
  import { FunctionComponent, ReactNode } from 'react';
2
2
  import { StateCreator } from 'zustand';
3
3
  import { defGetterStateType, optionsType, ExtraGetterState, insideActionsType } from './types';
4
- export declare function creator<S extends Record<string, any>, G extends defGetterStateType<S>, OptionActions extends Record<string, any>>(options: optionsType<S, G, OptionActions>): StateCreator<S & ExtraGetterState<S, G> & insideActionsType<S>>;
5
- export declare function defineStore<S extends Record<string, any>, G extends defGetterStateType<S>, OptionActions extends Record<string, any>>(options: optionsType<S, G, OptionActions>): import("zustand").UseBoundStore<import("zustand").StoreApi<S & ExtraGetterState<S, G> & insideActionsType<S>>>;
4
+ export declare function defineStateFactory<Options extends optionsType<any, any, any>>(options: Options): () => Options;
5
+ export declare function creator<S extends Record<string, any>, G extends defGetterStateType<S>, OptionActions extends Record<string, any>>(options: optionsType<S, G, OptionActions>): StateCreator<S & ExtraGetterState<S, G> & insideActionsType<S> & OptionActions>;
6
+ export declare function defineStore<S extends Record<string, any>, G extends defGetterStateType<S>, OptionActions extends Record<string, any>>(options: optionsType<S, G, OptionActions>): import("zustand").UseBoundStore<import("zustand").StoreApi<S & ExtraGetterState<S, G> & insideActionsType<S> & OptionActions>>;
6
7
  export declare function defineContext<S extends Record<string, any>, G extends defGetterStateType<S>, OptionActions extends Record<string, any>>(options: optionsType<S, G, OptionActions>): {
7
8
  Provider: FunctionComponent<{
8
9
  children?: ReactNode;
9
10
  }>;
10
- useContext: <T>(selector: (state: S & ExtraGetterState<S, G> & insideActionsType<S>) => T) => T;
11
+ useContext: <T>(selector: (state: S & ExtraGetterState<S, G> & insideActionsType<S> & OptionActions) => T) => T;
11
12
  };
package/dist/utils.js CHANGED
@@ -4,6 +4,9 @@ import { produce } from 'immer';
4
4
  import { keys, isEqualWith, omit, some, pick, forEach, isEqual } from 'lodash-es';
5
5
  import { jsx } from 'react/jsx-runtime';
6
6
 
7
+ function defineStateFactory(options) {
8
+ return () => options;
9
+ }
7
10
  function creator(options) {
8
11
  function createDefState() {
9
12
  const state = options.state();
@@ -77,4 +80,4 @@ function defineContext(options) {
77
80
  };
78
81
  }
79
82
 
80
- export { creator, defineContext, defineStore };
83
+ export { creator, defineContext, defineStateFactory, defineStore };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "define-zustand",
3
- "version": "3.0.3",
3
+ "version": "3.1.0",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",