File tree Expand file tree Collapse file tree 1 file changed +8
-20
lines changed
packages/hooks/src/createUseStorageState Expand file tree Collapse file tree 1 file changed +8
-20
lines changed Original file line number Diff line number Diff line change @@ -19,12 +19,14 @@ export interface Options<T> {
1919export function createUseStorageState ( getStorage : ( ) => Storage | undefined ) {
2020 function useStorageState < T > ( key : string , options : Options < T > = { } ) {
2121 let storage : Storage | undefined ;
22- const {
23- listenStorageChange = false ,
24- onError = ( e ) => {
25- console . error ( e ) ;
26- } ,
27- } = options ;
22+
23+ const { listenStorageChange = false } = options ;
24+
25+ const serializer = isFunction ( options . serializer ) ? options . serializer : JSON . stringify ;
26+
27+ const deserializer = isFunction ( options . deserializer ) ? options . deserializer : JSON . parse ;
28+
29+ const onError = isFunction ( options . onError ) ? options . onError : console . error ;
2830
2931 // https://github.com/alibaba/hooks/issues/800
3032 try {
@@ -33,20 +35,6 @@ export function createUseStorageState(getStorage: () => Storage | undefined) {
3335 onError ( err ) ;
3436 }
3537
36- const serializer = ( value : T ) => {
37- if ( options . serializer ) {
38- return options . serializer ( value ) ;
39- }
40- return JSON . stringify ( value ) ;
41- } ;
42-
43- const deserializer = ( value : string ) => {
44- if ( options . deserializer ) {
45- return options . deserializer ( value ) ;
46- }
47- return JSON . parse ( value ) ;
48- } ;
49-
5038 function getStoredValue ( ) {
5139 try {
5240 const raw = storage ?. getItem ( key ) ;
You can’t perform that action at this time.
0 commit comments