Gathering detailed insights and metrics for ew-responsive-store
Gathering detailed insights and metrics for ew-responsive-store
Gathering detailed insights and metrics for ew-responsive-store
Gathering detailed insights and metrics for ew-responsive-store
npm install ew-responsive-store
Typescript
Module System
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
2
4
ew-responsive-store
Making Session Storage Data ReactiveIf your project requires using session storage (either localStorage
or sessionStorage
) to persist data, and you want the data to be retained after a page refresh while also automatically updating the view when the data changes, I highly recommend using the ew-responsive-store
library. It's under 1 KB in size and extremely easy to use. With just a single function call, you can make session storage data reactive, which can be applied to any framework-based project, even native JavaScript projects. The library also includes comprehensive unit tests and type inference.
First, you need to install the ew-responsive-store
package. You can install it using the following command:
1npm install ew-responsive-store --save-dev 2# Or using pnpm 3pnpm add ew-responsive-store 4# Or using yarn 5yarn add ew-responsive-store
The core of the ew-responsive-store
package exports two methods: parseStr
and useStorage
. The useStorage
method is used to make session storage data reactive.
You can use useStorage
to create reactive basic values. For example, let's say you have a counter stored in localStorage
:
1import { useStorage } from 'ew-responsive-store'; 2 3// Initialize the count with a default value of 0 4const count = useStorage('count', 0); 5 6// Modify the count value 7count.value++; // count value becomes 1
Vue Template Code:
1<template> 2 <p>{{ count }}</p> 3 <button @click="count++">Click Me</button> 4</template> 5 6<script setup> 7import { useStorage } from 'ew-responsive-store'; 8 9const count = useStorage('count', 0); 10</script>
At this point, the value of count
is stored in the browser's session storage, and it is reactive, meaning it will persist even after the page refreshes and the view will update automatically when the value changes.
You can also store reactive objects in a similar way:
1import { useStorage } from 'ew-responsive-store'; 2 3// Initialize the userInfo object 4const userInfo = useStorage('user', { name: 'eveningwater' }); 5 6// Modify the userInfo object 7userInfo.value.name = '夕水'; // userInfo's name property becomes '夕水'
Vue Template Code:
1<template> 2 <p>{{ userInfo.name }}</p> 3 <button @click="userInfo.name = '小张'">Click Me</button> 4</template> 5 6<script setup> 7import { useStorage } from 'ew-responsive-store'; 8 9const userInfo = useStorage('user', { name: 'eveningwater' }); 10</script>
When you change the name
property of userInfo
, the view will automatically update, and the data will be saved in session storage.
You can also store arrays, and they will be reactive as well:
1import { useStorage } from 'ew-responsive-store'; 2 3// Initialize an array 4const countList = useStorage('countList', [1, 2, 3]); 5 6// Modify the array 7countList.value.push(4); // The array becomes [1, 2, 3, 4]
Vue Template Code:
1<template> 2 <p v-for="item in countList" :key="item">{{ item }}</p> 3 <button @click="countList.pop()">Click Me</button> 4</template> 5 6<script setup> 7import { useStorage } from 'ew-responsive-store'; 8 9const countList = useStorage('countList', [1, 2, 3]); 10</script>
By default, useStorage
enables deep watching, which is useful for objects and arrays. If you're dealing with basic types and don't need deep watching, you can disable it by passing a third configuration parameter:
1import { useStorage } from 'ew-responsive-store'; 2 3// Initialize count with deep watching disabled 4const count = useStorage('count', 0, { deep: false }); 5 6// Modify count 7count.value++; // count value becomes 1
By default, useStorage
uses localStorage
for persistent storage. If you want to use sessionStorage
instead, you can specify it in the configuration:
1import { useStorage } from 'ew-responsive-store'; 2import { StoreType } from 'ew-responsive-store/typings/core/enum'; 3 4const count = useStorage('count', 0, { deep: false, storage: StoreType.SESSION }); 5 6// Modify count 7count.value++; // count value becomes 1, and the data is stored in sessionStorage
By default, useStorage
listens to changes in the initial value. If you don't want to watch the initial value changes, you can control it by passing the immediate
parameter:
1import { useStorage } from 'ew-responsive-store'; 2import { StoreType } from 'ew-responsive-store/typings/core/enum'; 3 4// Don't listen to changes in the initial value 5const count = useStorage('count', 0, { deep: false, immediate: false }); 6 7// Only the next change will be watched 8count.value++; // count value becomes 1
parseStr
MethodThe parseStr
method is used to parse string values. It provides two parsing modes:
EVAL
: Similar to the eval
method, which executes JavaScript code within the string.JSON
: Similar to JSON.parse
, useful for parsing JSON-formatted strings.1import { parseStr } from 'ew-responsive-store'; 2import { parseStrType } from 'ew-responsive-store/typings/core/enum'; 3 4// Parse a JSON string 5const testJSONData = parseStr('{"name":"eveningwater"}'); 6console.log(testJSONData); // { name: "eveningwater" } 7 8// Execute JavaScript code from a string 9const testEvalData = parseStr('console.log("hello, eveningwater")', parseStrType.EVAL); 10// The console will log: hello, eveningwater
Since ew-responsive-store
is built on Vue's reactive system, you can configure it in more advanced ways by passing different parameters for the underlying watch functionality. You can refer to the Vue Reactivity API documentation to learn more about the parameters and usage.
No vulnerabilities found.
No security vulnerabilities found.