> For the complete documentation index, see [llms.txt](https://devworkshops.gitbook.io/masteringvuejs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://devworkshops.gitbook.io/masteringvuejs/state-management/keeping-state-in-local-storage.md).

# Keeping State in Local Storage

Cool, the whole state management is great but if I refresh the page it's all gone, what's the solution for that? Well, there are a few solutions for that, one of them is to keep in local storage is ingest it when the application loads.

First we're going to create a couple of things in the main store file (index.js). Two actions which will read and write to local storage and a mutation which will set the state from what comes from local storage.

```javascript
...
mutations: {
    SET_STATE(state, payload) {
        let keys = Object.keys(state)
        keys.forEach(key => {
            state[key] = payload[key]
        })
    }
},
actions: {
    ReadInitialStateFromLocalStorage({ commit }) {
        let state = localStorage.getItem('state')
        if (state) commit('SET_STATE', JSON.parse(state))
    },
    StoreInLocalStorage({ state }) {
        localStorage.setItem('state', JSON.stringify(state))
    }
}
...
```

Now, in the **App.vue** component we're going to dispatch the action to read from local storage as soon as it's created

```javascript
...
created() {
    ...
    this.$store.dispatch('ReadInitialStateFromLocalStorage')
},
...
```

Now the tricky part is when we  should write to local storage, again there are a few options. The one we're going for is as soon as the user navigates from one route to another. So let's add this snippet to our **main.js** file just before we create the vue instance.

{% code title="main.js" %}

```javascript
...
router.beforeEach((to, from, next) => {
    if (from.name) {
        store.dispatch('StoreInLocalStorage')
    }
    next()
})
...
```

{% endcode %}

Great! Once the suppliers are in the store we can navigate out, refresh the page and they are still going to be there.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://devworkshops.gitbook.io/masteringvuejs/state-management/keeping-state-in-local-storage.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
