question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Persist store throughout page reloads

See original GitHub issue

What problem is this solving

To completely eliminate the need for Vuex, since Vuex has session persistence support via a plugin.

Proposed solution

Accept an extra object in the createStore initialization:

export const usePiniaStore = createStore({
	id: "simpleStore",
	state: () => ({
		simpleVariable: "",
	}),
	persistence: {
		enable: true,
		mode: "localSession"
	}
});

// type definition
interface IPiniaPersistence {
	enable: boolean,
	mode: "localSession" | "localStorage";		
}

Describe alternatives you’ve considered

There does not seem to be any Vuex-less alternatives out there that supports the vue-composition-api.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

18reactions
posvacommented, Jan 11, 2022

An updated and simpler version is to use something like https://vueuse.org/core/uselocalstorage/#uselocalstorage:

defineStore('store', {
  state: () => ({
    count: useLocalStorage('count', 0)
  })
})
18reactions
posvacommented, Apr 28, 2021

Vuex doesn’t have persistence support, it comes from a plugin. For Pinia, it would be the same. This can be implemented by using store.$subscribe a watch(pinia.state, handler, { deep: true }) to write to local storage (or anything else) and watching localStorage for changes and writing back to the store.

Something along the lines of

// save the whole state
watch(pinia.state, (state) => {
  localStorage.setItem('state', JSON.stringify(state)
}, { deep: true })

// save cart store
watch(() => pinia.state.value.cart, (state) => {
  localStorage.setItem('cart', JSON.stringify(state)
}, { deep: true })

You can inspire yourself from https://github.com/robinvdvleuten/vuex-persistedstate/blob/master/src/index.ts to create it

There is also https://github.com/posva/pinia/issues/191 going on to discuss plugins, they will probably be similar to Vuex 5 plugins once that is discussed too

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I persist redux state tree on refresh? - Stack Overflow
If you would like to persist your redux state across a browser refresh, it's best to do this using redux middleware.
Read more >
5 Methods to Persisting State Between Page Reloads in React
One of the straightforward options is to use localStorage in the browser to persist the state. Let's take a look at an example....
Read more >
React Redux Persist State on refresh || Keep the user logged in
Today we shall learn how to persist the redux state on page refresh or Keep the user logged in. React Redux tutorial: ...
Read more >
How to Save State to LocalStorage & Persist on Refresh with ...
I've preloaded the application with some data from spacejelly.dev where at that point I'm simply looping through each post and rendering them on ......
Read more >
Persist store throughout page reloads · Issue #309 · vuejs/pinia
What problem is this solving To completely eliminate the need for Vuex, since Vuex has session persistence support via a plugin. Proposed solution...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found