How to store a value permanently?
JEricaM opened this issue · comments
If I store a variable inside the store.ts why, when I refresh the page, the data it's reset to the default value that is in the store? It shouldn't be remain the same value unless I change it in the code?
I use state.variable = value to store inside my code. Maybe I should use something different?
This is my store
`import { createStore } from "@stencil/store";
const { state } = createStore({
accountname: "",
email: "",
usertype: "",
clientId: 0,
});
export default state;
`
and this is how I assign the variable in my code
state.clientId = 1;
Thank you
Hello @JEricaM!
Stores' values are stored in memory, thus they are ephemeral and they don't persist between reloads. Think of them as @State()
s on steroids.
If you want to persist the data locally, you can use stencil-store-storage
by yours truly. It's really simple to use the adapters for localStorage
and sessionStorage
. If you need something else, you can provide your own storage.
Also, for the localStorage
adapter you can ask it to be synced between tabs!
Thanks a lot, I didn't fully understand the functionality of stencil store from the doc.
Now it's clear! I think that I will use localStorage when "onChange" in my store it's executed.
Thank again.
Have a good day.
If you only have one property, that is the fastest way. If you have a bigger object, consider using the package.
Thank you!