Vue function that provides way to read, update and delete a sessionStorage key
interface StorageOptions {
isParsing: boolean
serializer?: SerializerFunction
deserializer?: DeserializerFunction
}
function useSessionStorage(
key: string,
options?: StorageOptions,
runOnMount?: boolean
): {
item: Ref<any>
getItem: () => void
setItem: (newVal: any) => void
removeItem: () => void
}
key: string
the sessionStorage key you wish to get/set/removeoptions: StorageOptions
isParsing: boolean
whether to enable parsing the sessionStorage key value or not,false
by defaultserializer: SerializerFunction
a custom serializer,JSON.stringify
by defaultdeserializer: DeserializerFunction
a custom deserializer,JSON.parse
by default
runOnMount: boolean
whether to get the sessionStorage key on mount or not,true
by default
item: Ref<any>
the sessionStorage key value, it can be null, a string or a JSON object/arraygetItem: Function
get the sessionStorage key valuesetItem: Function
set the sessionStorage key valuenewVal: any
: the value to set, can be a string or an object/array
removeItem: Function
delete the sessionStorage key
<template>
<div>
<div>Item: {{ item }}</div>
<button @click="getItem">Get item</button>
<button @click="setItem('Value here')">Set item</button>
<button @click="removeItem">Remove item</button>
</div>
</template>
<script lang="ts">
import Vue from 'vue'
import { useSessionStorage } from 'vue-use-kit'
export default Vue.extend({
name: 'UseSessionStorageDemo',
setup() {
const { item, getItem, setItem, removeItem } = useSessionStorage(
'i_love_session_storage'
)
return {
item,
getItem,
setItem,
removeItem
}
}
})
</script>