-
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.
feat: Add custom hook to store useState values in local and session s…
…torage.
- Loading branch information
1 parent
8e23125
commit fd33702
Showing
1 changed file
with
84 additions
and
0 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,84 @@ | ||
import { useState, useEffect, Dispatch, SetStateAction } from "react"; | ||
|
||
export const useSessionStorage = <T,>( | ||
key: string, | ||
initialValue: T | ||
): [T, Dispatch<SetStateAction<T>>] => { | ||
// Function to get the current value from sessionStorage or default to initialValue | ||
const getStoredValue = (): T => { | ||
const storedValue = sessionStorage.getItem(key); | ||
|
||
if (storedValue) { | ||
try { | ||
return JSON.parse(storedValue) as T; | ||
} catch (error) { | ||
console.error( | ||
"[useSessionStorage][getStoredValue]:", | ||
"Error parsing session storage value", | ||
error | ||
); | ||
} | ||
} | ||
return initialValue; | ||
}; | ||
|
||
// Use useState with the initial value from sessionStorage | ||
const [value, setValue] = useState<T>(getStoredValue); | ||
|
||
// Sync the value to sessionStorage whenever it changes | ||
useEffect(() => { | ||
try { | ||
sessionStorage.setItem(key, JSON.stringify(value)); | ||
} catch (error) { | ||
console.error( | ||
"[useSessionStorage][useEffect]:", | ||
"Error setting session storage value", | ||
error | ||
); | ||
} | ||
}, [key, value]); | ||
|
||
// Return the state and setter function | ||
return [value, setValue]; | ||
}; | ||
|
||
export const useLocalStorage = <T,>( | ||
key: string, | ||
initialValue: T | ||
): [T, Dispatch<SetStateAction<T>>] => { | ||
// Function to get the current value from localStorage or default to initialValue | ||
const getStoredValue = (): T => { | ||
const storedValue = localStorage.getItem(key); | ||
if (storedValue) { | ||
try { | ||
return JSON.parse(storedValue) as T; | ||
} catch (error) { | ||
console.error( | ||
"[useLocalStorage][getStoredValue]:", | ||
"Error parsing local storage value", | ||
error | ||
); | ||
} | ||
} | ||
return initialValue; | ||
}; | ||
|
||
// Use useState with the initial value from localStorage | ||
const [value, setValue] = useState<T>(getStoredValue); | ||
|
||
// Sync the value to localStorage whenever it changes | ||
useEffect(() => { | ||
try { | ||
localStorage.setItem(key, JSON.stringify(value)); | ||
} catch (error) { | ||
console.error( | ||
"[useLocalStorage][useEffect]:", | ||
"Error setting local storage value", | ||
error | ||
); | ||
} | ||
}, [key, value]); | ||
|
||
// Return the state and setter function | ||
return [value, setValue]; | ||
}; |