How to create a reactive localstorage?

Hi, I need to create a reactive localstorage for localization on my site but I don't know how to do it, here is my attempt to make one:
const [locale, setLocale] = createStore({
locale: "en",
});
const [localeJson, setLocaleJson] = createSignal(locales[locale.locale]);
createEffect(() => {
setLocaleJson(locales[locale.locale]);
}, locale.locale);
const [locale, setLocale] = createStore({
locale: "en",
});
const [localeJson, setLocaleJson] = createSignal(locales[locale.locale]);
createEffect(() => {
setLocaleJson(locales[locale.locale]);
}, locale.locale);
but it doesn't work because when the page reloads, the value reverts to the original value
25 Replies
thetarnav
thetarnav6mo ago
the most straightforward way is to just write to local storage when you write to a signal/store and then read from local storage to get the initial value during creating
Дикий
Дикий6mo ago
This option is not suitable because I have a lot of components and only in the component where the language is changed it will work.
deluksic
deluksic6mo ago
This signal would usually be global, so it should work in other components as well
Дикий
Дикий6mo ago
So there is no way without contexts, like you can't just track changes to locastorage?
deluksic
deluksic6mo ago
There is no built in way, but it should be really simple to do. Maybe you want to check out https://primitives.solidjs.community/package/storage#createLocalStorage
Solid Primitives
A library of high-quality primitives that extend SolidJS reactivity
thetarnav
thetarnav6mo ago
you can there is some event listener for that but since you are causing the change you know when it happens also you a global signal doesn’t have to mean context just create it somewhere and import in all files needed
Дикий
Дикий6mo ago
I installed it, but it's not there. createStorage
No description
deluksic
deluksic6mo ago
yeah, seems like you would use makePersisted, createLocalStorage does not exist anymore? or is just deprecated
Дикий
Дикий6mo ago
they don't exist
deluksic
deluksic6mo ago
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
Дикий
Дикий6mo ago
No description
deluksic
deluksic6mo ago
how did you install it?
Дикий
Дикий6mo ago
using npm npm install @solid-primitives/storage
deluksic
deluksic6mo ago
Make sure you have "@solid-primitives/storage": "2.1.4" in your package.json dependencies, then npm install:
No description
deluksic
deluksic6mo ago
can't help more than that I'm afraid
Want results from more Discord servers?
Add your server