refactor(hooks): add comments to local storage hook
Some checks failed
Open PR on Staging / audit_and_check (pull_request) Failing after 16s
Some checks failed
Open PR on Staging / audit_and_check (pull_request) Failing after 16s
This commit is contained in:
parent
afdc9449b1
commit
4f5dcc0336
@ -6,6 +6,11 @@ import {
|
||||
setLocalStorageItem
|
||||
} from '../utils'
|
||||
|
||||
/**
|
||||
* Subscribe to the Browser's storage event. Get the new value if any of the tabs changes it.
|
||||
* @param callback - function to be called when the storage event is triggered
|
||||
* @returns clean up function
|
||||
*/
|
||||
const useLocalStorageSubscribe = (callback: () => void) => {
|
||||
window.addEventListener('storage', callback)
|
||||
return () => window.removeEventListener('storage', callback)
|
||||
@ -28,8 +33,11 @@ export function useLocalStorage<T>(
|
||||
)
|
||||
}
|
||||
|
||||
// https://react.dev/reference/react/useSyncExternalStore
|
||||
// Returns the snapshot of the data and subscribes to the storage event
|
||||
const data = React.useSyncExternalStore(useLocalStorageSubscribe, getSnapshot)
|
||||
|
||||
// Takes the value or a function that returns the value and updates the local storage
|
||||
const setState: React.Dispatch<React.SetStateAction<T>> = React.useCallback(
|
||||
(v: React.SetStateAction<T>) => {
|
||||
try {
|
||||
|
Loading…
x
Reference in New Issue
Block a user