File tree Expand file tree Collapse file tree 5 files changed +103
-0
lines changed Expand file tree Collapse file tree 5 files changed +103
-0
lines changed Original file line number Diff line number Diff line change @@ -183,6 +183,7 @@ function getHooksSidebar() {
183
183
{ text : 'useSessionStorageState' , link : '/useSessionStorageState/' } ,
184
184
{ text : 'useMap' , link : '/useMap/' } ,
185
185
{ text : 'useSet' , link : '/useSet/' } ,
186
+ { text : 'useSetState' , link : '/useSetState/' } ,
186
187
] ,
187
188
} ,
188
189
Original file line number Diff line number Diff line change @@ -29,6 +29,7 @@ import useMedia from './useMedia'
29
29
import useNetwork from './useNetwork'
30
30
import useSessionStorageState from './useSessionStorageState'
31
31
import useSet from './useSet'
32
+ import useSetState from './useSetState'
32
33
import useSize from './useSize'
33
34
import useScroll from './useScroll'
34
35
import useToggle from './useToggle'
@@ -76,6 +77,7 @@ export {
76
77
usePreview ,
77
78
useSessionStorageState ,
78
79
useSet ,
80
+ useSetState ,
79
81
useSize ,
80
82
useScroll ,
81
83
useToggle ,
Original file line number Diff line number Diff line change
1
+ <template >
2
+ <div >{{ JSON.stringify(state) }}</div >
3
+ <div class =" contain" >
4
+ <vhp-button @click =" initAge()" >
5
+ add age
6
+ </vhp-button >
7
+
8
+ <vhp-button @click =" updateAge()" >
9
+ update age
10
+ </vhp-button >
11
+
12
+ <vhp-button @click =" addTag()" >
13
+ add tag
14
+ </vhp-button >
15
+ </div >
16
+ </template >
17
+
18
+ <script lang="ts" setup>
19
+ import { useSetState } from ' vue-hooks-plus'
20
+
21
+ const [state, setState] = useSetState ({ name: ' vue-hooks-plus' })
22
+
23
+ const initAge = () => {
24
+ setState ({
25
+ age: 1 ,
26
+ })
27
+ }
28
+
29
+ const addTag = () => {
30
+ setState ({
31
+ ... state .value ,
32
+ age: 3 ,
33
+ tag: ' nice' ,
34
+ })
35
+ }
36
+
37
+ const updateAge = () => {
38
+ setState ({
39
+ age: 2 ,
40
+ })
41
+ }
42
+ </script >
43
+
44
+ <style scoped lang="less">
45
+ .contain {
46
+ button {
47
+ margin-right : 8px ;
48
+ }
49
+ }
50
+ </style >
Original file line number Diff line number Diff line change
1
+ ---
2
+ map :
3
+ # 映射到docs的路径
4
+ path : /useSetState
5
+ ---
6
+
7
+ # useSetState
8
+
9
+ 管理 object 类型 响应式 的 Hooks,支持解构赋值,更方便维护。
10
+
11
+ ## 代码演示
12
+
13
+ ### 基础用法
14
+
15
+ <demo src="./demo/demo.vue"
16
+ language="vue"
17
+ title="基本用法"
18
+ desc=""> </demo >
19
+
20
+ ## API
21
+
22
+ ``` typescript
23
+ const [state, setState] = useSetState < S extends Record < string , any>>
24
+ (initialState : StateType <S >)
25
+ :[
26
+ [S] extends [Ref<any >] ? S : Ref<UnwrapRef <S >>,
27
+ (patch : Record <string , any >) => void
28
+ ]
29
+ ```
Original file line number Diff line number Diff line change
1
+ import { ref , Ref , unref , UnwrapRef } from 'vue'
2
+ import { merge } from 'lodash'
3
+
4
+ type StateType < S > = S | ( ( ) => S ) | Ref < S > | ( ( ) => Ref < S > )
5
+
6
+ function useSetState < S extends Record < string , any > > (
7
+ initialState : StateType < S > ,
8
+ ) : [ [ S ] extends [ Ref < any > ] ? S : Ref < UnwrapRef < S > > , ( patch : Record < string , any > ) => void ] {
9
+ const getInitialState = ( ) => unref ( initialState )
10
+
11
+ const state = ref < S > ( getInitialState ( ) as S )
12
+
13
+ const setMergeState = ( patch : Record < string , any > ) => {
14
+ const newState = unref ( patch )
15
+ state . value = newState ? merge ( state . value , newState ) : state . value
16
+ }
17
+
18
+ return [ state , setMergeState ]
19
+ }
20
+
21
+ export default useSetState
You can’t perform that action at this time.
0 commit comments