From 341f0fae65989dd2dd20b3089b550ff5817775aa Mon Sep 17 00:00:00 2001 From: Walidoux Date: Thu, 4 May 2023 16:14:11 +0100 Subject: [PATCH] feat(hooks): add useLocalStorage --- src/hooks/useLocalStorage.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/hooks/useLocalStorage.ts diff --git a/src/hooks/useLocalStorage.ts b/src/hooks/useLocalStorage.ts new file mode 100644 index 0000000..bc18d13 --- /dev/null +++ b/src/hooks/useLocalStorage.ts @@ -0,0 +1,18 @@ +import type { Accessor } from 'solid-js' +import { createSignal } from 'solid-js' + +export interface ILocalStorage { + lang: Accessor + setLang: (domain: string) => void +} + +const [lang, setLangeState] = createSignal((localStorage.getItem('LANG_KEY') as string) ?? 'com') + +const setLang: ILocalStorage['setLang'] = (domain) => { + localStorage.setItem('LANG_KEY', domain) + return setLangeState(domain) +} + +export const useLocalStorage = (): ILocalStorage => { + return { lang, setLang } +}