-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: hooks usePersistentState and usePrevious
- Loading branch information
Showing
6 changed files
with
115 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { useState, useEffect } from "react"; | ||
|
||
export function usePersistentState<Type>( | ||
key: string, | ||
initialState: Type | (() => Type), | ||
options: { storagePrefix?: string; storage?: Storage } = {} | ||
): [Type, React.Dispatch<React.SetStateAction<Type>>] { | ||
const { storagePrefix = "use-persistent-state-", storage = localStorage } = | ||
options; | ||
const prefixedKey = storagePrefix + key; | ||
// read key from the specified storage if not found use default value | ||
const [value, setValue] = useState<Type>(() => { | ||
const storedValue = storage.getItem(prefixedKey); | ||
if (storedValue === null) { | ||
if (typeof initialState === "function") { | ||
return (initialState as () => Type)(); | ||
} else { | ||
return initialState; | ||
} | ||
} else { | ||
return JSON.parse(storedValue); | ||
} | ||
}); | ||
// update the specified storage when value changes | ||
useEffect(() => { | ||
storage.setItem(prefixedKey, JSON.stringify(value)); | ||
}, [value, prefixedKey, storage]); | ||
return [value, setValue]; | ||
} |
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,9 @@ | ||
import { useEffect, useRef } from "react"; | ||
|
||
export function usePrevious<T>(value: T): T | undefined { | ||
const ref = useRef<T>(); | ||
useEffect(() => { | ||
ref.current = value; | ||
}, [value]); | ||
return ref.current; | ||
} |
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.