ionic-team / stencil-store

Store is a lightweight shared state library by the StencilJS core team. Implements a simple key/value map that efficiently re-renders components when necessary.

Home Page:https://stenciljs.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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!

2020-06-17 13 44 34

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!

This should really be documented in the docs. It's not clear that the store doesn't last past reloads.