Open History to see the last commit
1import * as Solid from 'solid-js'
2β
3export function useSessionStorage<T>(key: string, initialValue: any) {
4 const [state, setState] = Solid.createSignal<T>(
5 (() => {
6 const stored = sessionStorage.getItem(key)
7 console.log('stored', key, stored, initialValue, typeof stored)
8 const reutrnval =
9 stored && stored !== 'undefined' ? JSON.parse(stored) : initialValue
10 console.log('returning', reutrnval)
11 return reutrnval
12 })(),
13 )
14β
15 Solid.createEffect(() => {
16 console.log('setting', JSON.stringify(state()))
17 sessionStorage.setItem(key, JSON.stringify(state()))
18 })
19β
20 return [state, setState]
21}