forked from alibaba/hooks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request alibaba#770 from alibaba/refactor/use-storage-state
refactor: useStorageState -> createUseStorageState
- Loading branch information
Showing
5 changed files
with
83 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { useState, useCallback } from 'react'; | ||
import useUpdateEffect from '../useUpdateEffect'; | ||
|
||
export interface IFuncUpdater<T> { | ||
(previousState?: T): T; | ||
} | ||
|
||
export interface IFuncStorage { | ||
(): Storage; | ||
} | ||
|
||
export type StorageStateResult<T> = [T | undefined, (value?: T | IFuncUpdater<T>) => void]; | ||
|
||
function isFunction<T>(obj: any): obj is T { | ||
return typeof obj === 'function'; | ||
} | ||
|
||
export function createUseStorageState(nullishStorage: Storage | null) { | ||
function useStorageState<T>( | ||
key: string, | ||
defaultValue?: T | IFuncUpdater<T>, | ||
): StorageStateResult<T> { | ||
const storage = nullishStorage as Storage; | ||
const [state, setState] = useState<T | undefined>(() => getStoredValue()); | ||
useUpdateEffect(() => { | ||
setState(getStoredValue()); | ||
}, [key]); | ||
|
||
function getStoredValue() { | ||
const raw = storage.getItem(key); | ||
if (raw) { | ||
try { | ||
return JSON.parse(raw); | ||
} catch (e) {} | ||
} | ||
if (isFunction<IFuncUpdater<T>>(defaultValue)) { | ||
return defaultValue(); | ||
} | ||
return defaultValue; | ||
} | ||
|
||
const updateState = useCallback( | ||
(value?: T | IFuncUpdater<T>) => { | ||
if (typeof value === 'undefined') { | ||
storage.removeItem(key); | ||
setState(undefined); | ||
} else if (isFunction<IFuncUpdater<T>>(value)) { | ||
const previousState = getStoredValue(); | ||
const currentState = value(previousState); | ||
storage.setItem(key, JSON.stringify(currentState)); | ||
setState(currentState); | ||
} else { | ||
storage.setItem(key, JSON.stringify(value)); | ||
setState(value); | ||
} | ||
}, | ||
[key], | ||
); | ||
|
||
return [state, updateState]; | ||
} | ||
if (!nullishStorage) { | ||
return function (defaultValue: any) { | ||
return [ | ||
isFunction<IFuncUpdater<any>>(defaultValue) ? defaultValue() : defaultValue, | ||
() => {}, | ||
]; | ||
} as typeof useStorageState; | ||
} | ||
return useStorageState; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,7 @@ | ||
import useStorageState from '../useStorageState'; | ||
import { createUseStorageState } from '../createUseStorageState'; | ||
|
||
function useLocalStorageState<T = undefined>( | ||
key: string, | ||
): [T | undefined, (value?: T | ((previousState?: T) => T)) => void]; | ||
|
||
function useLocalStorageState<T>( | ||
key: string, | ||
defaultValue: T | (() => T), | ||
): [T, (value?: T | ((previousState: T) => T)) => void]; | ||
|
||
function useLocalStorageState<T>(key: string, defaultValue?: T | (() => T)) { | ||
return useStorageState(() => localStorage, key, defaultValue); | ||
} | ||
const useLocalStorageState = createUseStorageState( | ||
typeof window === 'object' ? window.localStorage : null, | ||
); | ||
|
||
export default useLocalStorageState; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,7 @@ | ||
import useStorageState from '../useStorageState'; | ||
import { createUseStorageState } from '../createUseStorageState'; | ||
|
||
function useSessionStorageState<T = undefined>( | ||
key: string, | ||
): [T | undefined, (value?: T | ((previousState?: T) => T)) => void]; | ||
|
||
function useSessionStorageState<T>( | ||
key: string, | ||
defaultValue: T | (() => T), | ||
): [T, (value?: T | ((previousState: T) => T)) => void]; | ||
|
||
function useSessionStorageState<T>(key: string, defaultValue?: T | (() => T)) { | ||
return useStorageState(() => sessionStorage, key, defaultValue); | ||
} | ||
const useSessionStorageState = createUseStorageState( | ||
typeof window === 'object' ? window.sessionStorage : null, | ||
); | ||
|
||
export default useSessionStorageState; |
This file was deleted.
Oops, something went wrong.