S
SolidJSβ€’2y ago
Dikiy

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
thetarnavβ€’2y 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
Dikiy
DikiyOPβ€’2y 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
deluksicβ€’2y ago
This signal would usually be global, so it should work in other components as well
Dikiy
DikiyOPβ€’2y ago
So there is no way without contexts, like you can't just track changes to locastorage?
deluksic
deluksicβ€’2y 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
thetarnavβ€’2y 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
Dikiy
DikiyOPβ€’2y ago
I installed it, but it's not there. createStorage
No description
deluksic
deluksicβ€’2y ago
yeah, seems like you would use makePersisted, createLocalStorage does not exist anymore? or is just deprecated
Dikiy
DikiyOPβ€’2y ago
they don't exist
deluksic
deluksicβ€’2y ago
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
Dikiy
DikiyOPβ€’2y ago
No description
deluksic
deluksicβ€’2y ago
how did you install it?
Dikiy
DikiyOPβ€’2y ago
using npm npm install @solid-primitives/storage
deluksic
deluksicβ€’2y ago
Make sure you have "@solid-primitives/storage": "2.1.4" in your package.json dependencies, then npm install:
No description
deluksic
deluksicβ€’2y ago
can't help more than that I'm afraid
Dikiy
DikiyOPβ€’2y ago
nothing
No description
deluksic
deluksicβ€’2y ago
can you try directly importing like I showed?
Dikiy
DikiyOPβ€’2y ago
No description
Dikiy
DikiyOPβ€’2y ago
ok wow, it's working.
deluksic
deluksicβ€’2y ago
yeah ts can get wonky
Dikiy
DikiyOPβ€’2y ago
he was able to find this function
deluksic
deluksicβ€’2y ago
But an important thing: you will still want to makePersisted once in your application and not in each component. then you can just import the getter and setter you get from makePersisted into other components need to get back to work! See you later πŸ‘‹
Dikiy
DikiyOPβ€’2y ago
okay bye πŸ€‘ Thanks a lot πŸ€©πŸ€©πŸ€©πŸ€©πŸ˜‹πŸ˜‹πŸ˜‹πŸ˜‹πŸ€‘πŸ€‘
ELSamu
ELSamuβ€’2y ago
Heres a hook that subscribes for events like store event that fires when theres a change in the value stored
No description
ELSamu
ELSamuβ€’2y ago
Then just use it
No description

Did you find this page helpful?