useSessionStorageState
State synchronized with sessionStorage.
State synchronized with sessionStorage.
Import
import { useSessionStorageState } from '@kamod-ch/hooks'Live demo
Basic demo
State synchronized with sessionStorage.
Browser-only demo
This demo starts after the page mounts in the browser.
import { useSessionStorageState } from '@kamod-ch/hooks'
const key = 'kamod-hooks-demo-use-session-storage-state'
export default function UseSessionStorageStateDemo() {
const [value, setValue] = useSessionStorageState<string>(key, { defaultValue: 'session' })
return (
<div>
<input value={value} onInput={(event) => setValue(event.currentTarget.value)} />
<p><strong>Stored value:</strong> {value}</p>
<button type="button" onClick={() => setValue(undefined)}>Clear</button>
</div>
)
}
API
TypeScript signature
declare const useSessionStorageState: <T>(key: string, options?: import("../createUseStorageState/index.js").Options<T>) => readonly [T, (this: unknown, value: import("../createUseStorageState/index.js").SetState<T>) => void];
export default useSessionStorageState;
/SSR considerations
This hook touches browser APIs. Render it after mount or guard it with BrowserOnly in SSR and static builds.
Browser compatibility
Requires the corresponding browser API to exist in the current environment.