Open History to see the last commit
1import * as React from 'react'
2β
3export function useSessionStorage<T>(key: string, initialValue: T) {
4 const state = React.useState<T>(() => {
5 const stored = sessionStorage.getItem(key)
6 return stored ? JSON.parse(stored) : initialValue
7 })
8β
9 React.useEffect(() => {
10 sessionStorage.setItem(key, JSON.stringify(state[0]))
11 }, [state[0]])
12β
13 return state
14}