# LocalStorage

LocalStorage (opens new window).

# Parameters

import { useLocalStorage } from "vue-composable";

const localStorage = useLocalStorage(key, defaultValue?, sync?);
Parameters Type Required Default Description
key string, ref<string> true Key that will be used to store in localStorage
defaultValue object false undefined default value stored in the localStorage
sync Boolean false true sets the storage to sync automatically between tabs

# State

The useLocalStorage function exposes the following reactive state:

import { useLocalStorage } from "vue-composable";

const { supported, storage } = useLocalStorage(key);
State Type Description
supported boolean returns true is localStorage is available
storage Ref<any> handler with localStorage value

# Methods

The useLocalStorage function exposes the following methods:

import { useLocalStorage } from "vue-composable";

const { remove, clear, setSync } = useLocalStorage(key);
Signature Description
remove() Removes key from the localStorage, equivalent as storage.value = undefined
clear() Clears all used localStorage used so far
setSync(boolean) Update the local storage if the value is changed in other tab

# Sync


When using sync: true, only the last ref per key will be able to update

# Example

Local Storage Cross tab

localStorage: 1

supported: false

Check the value in the dev tools: `__vue_localStorage_example`

Enable tab sync?

# Code

    localStorage: {{ storage }}
      <b :class="{ green: supported, red: !supported }">{{ supported }}</b>
      <b>Check the value in the dev tools: `{{ key }}`</b>
    <label for="storage">
      <input name="storage" v-model="storage" />

      <p>Enable tab sync? <input type="checkbox" v-model="tabSync" /></p>
      <p v-if="tabSync">
        Now this tab is listening for changes, please change the storage value
        in other tab
      <button @click="remove">Remove</button>

import { useLocalStorage } from "vue-composable";
import { ref, watch } from "@vue/composition-api";
export default {
  name: "local-storage-example",

  setup() {
    const key = "__vue_localStorage_example";
    const tabSync = ref(false);
    const { supported, storage, setSync, remove } = useLocalStorage(key, 1);

    watch(tabSync, setSync);

    return {

.red {
  color: red;
.green {
  color: green;

# Notes

This uses WebStorage, if access to the internal store is required, use useWebStorage('localStorage').store to access it.