How to Persist Data in the Pinia Store
In this lesson, we will see how to persist data in the Pinia store.
When working with Pinia to manage the state we want to keep the data when the user refreshes the browser, so how can we do that?
Install the package
First, we need to install this package.
Once done import and use the package inside your main.js file.
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
pinia.use(piniaPluginPersistedstate)
Persist the data
All you need to do as the code below shows is to add "persist: true" after the state object.
import { defineStore } from "pinia"
export const useCartStore = defineStore("cart", { state: () => ({ cartItems: [], }), persist: true, //})