killua is a local-storage management library for React applications.
Installations
npm install killua-beta
Developer Guide
Typescript
Yes
Module System
CommonJS, ESM
Score
73.6
Supply Chain
98.6
Quality
79.8
Maintenance
100
Vulnerability
100
License
Contributors
Unable to fetch Contributors
Languages
TypeScript (82.63%)
MDX (7.59%)
CSS (6.17%)
JavaScript (2.63%)
HTML (0.85%)
Shell (0.12%)
Developer
Download Statistics
Total Downloads
625
Last Day
1
Last Week
13
Last Month
22
Last Year
625
GitHub Statistics
61 Stars
288 Commits
2 Forks
1 Watching
1 Branches
2 Contributors
Bundle Size
8.81 kB
Minified
2.51 kB
Minified + Gzipped
Package Meta Information
Latest Version
0.0.6
Package Id
killua-beta@0.0.6
Unpacked Size
113.74 kB
Size
18.68 kB
File Count
78
Publised On
24 Feb 2024
Total Downloads
Cumulative downloads
Total Downloads
625
Last day
0%
1
Compared to previous day
Last week
0%
13
Compared to previous week
Last month
450%
22
Compared to previous month
Last year
0%
625
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
killua ·
killua is a local-storage management library for React applications.
Table of Contents
Installation
To install the latest stable version, run the following command:
1# npm 2npm install killua 3 4# pnpm 5pnpm install killua 6 7# yarn 8yarn add killua
Features
- Get data from localStorage
- Set data to localStorage
- Reducer for state management
- Selector for data access
- Expiration timer
- Schema validation
- Obfuscate data in localStorage
- Server-Side Compatibility
- TypeScript friendly
- Auto update in other tabs
- Auto update in other components
- Config file for configuration management
Usage
Create slice configuration file
that
slice
refers to a key within localStorage.
- Create a
slices
directory for the thunder configuration. - Create the slice configuration file, for example:
counter.ts
. - Set up the slice configuration:
1import { slice } from 'killua'; 2 3export const counterSlice = slice({ 4 key: 'counter', // unique key for localStorage 5 defaultClient: 1 as number // default value for client-side application 6});
Get data
Whatever the
useKillua
hook returns, theslice
function also returns.
Use the useKillua hook within the React framework, and as long as you don't have access to it, use the slice function.
- Within a React component, utilize the
useKillua
hook :
1import { useKillua } from "killua"; 2import { counterSlice } from "@/slices/counter"; 3 4export default function Component() { 5 const localstorageCounter = useKillua(counterSlice); 6 return ( 7 <div> 8 <p>{counterSlice.get()}</p> 9 </div> 10 ) 11};
- Outside of a React component, employ the
slice
function :
1import { counterSlice } from '@/slices/counter'; 2 3export function getCounter() { 4 return counterSlice.get(); 5}
Set data
Whatever the
useKillua
hook returns, theslice
function also returns.
Use the useKillua hook within the React framework, and as long as you don't have access to it, use the slice function.
- Within a React component, utilize the
useKillua
hook :
1import { useKillua } from "killua"; 2import { counterSlice } from "@/slices/counter"; 3 4export default function Component() { 5 const localstorageCounter = useKillua(counterSlice); 6 return ( 7 <div> 8 {/* without callback */} 9 <button onClick={() => counterSlice.set(0)}>Set counter to zero</button> 10 {/* with callback */} 11 <button onClick={() => counterSlice.set((prev) => prev + 1)}>Increment counter</button> 12 </div> 13 ) 14};
- Outside of a React component, employ the
slice
function :
1import { counterSlice } from '@/slices/counter'; 2 3// without callback 4export function setCounterToZero() { 5 counterSlice.set(0); 6} 7 8// with callback 9export function incrementCounter() { 10 counterSlice.set(prev => prev + 1); 11}
Use selectors
- To use a selector, simply add it to the slice config :
1import { slice } from 'killua'; 2 3export const counterSlice = slice({ 4 key: 'counter', 5 defaultClient: 1 as number, 6 selectors: { 7 getPlusOne: (value) => value + 1, 8 getPlusWithPayload: (value, payload: number) => value + payload 9 } 10});
- Within a React component, utilize the
useKillua
hook :
1import { useKillua } from "killua"; 2 3export default function Component() { 4 const localstorageCounter = useKillua(counterSlice); 5 return ( 6 <div> 7 <p>{localstorageCounter.getPlusOne()}</p> {/* without payload */} 8 <p>{localstorageCounter.getPlusWithPayload(5)}</p> {/* with payload */} 9 </div> 10 ) 11};
- Outside of a React component, employ the
slice
function :
1import { counterSlice } from '@/slices/counter'; 2 3// without payload 4export function getCounterWithPlusOne() { 5 return counterSlice.getPlusOne(); 6} 7 8// with payload 9export function getCounterWithPlusWithPayload() { 10 return counterSlice.getPlusWithPayload(5); 11}
Use reducers
- To use a reducer, simply add it to the slice config :
1import { slice } from 'killua'; 2 3export const counterSlice = slice({ 4 key: 'counter', 5 defaultClient: 1 as number, 6 reducers: { 7 increment: (value) => value + 1, 8 incrementWithPayload: (value, payload: number) => value + payload 9 } 10});
- Within a React component, utilize the
useKillua
hook :
1import { useKillua } from "killua"; 2 3export default function Component() { 4 const localstorageCounter = useKillua(counterSlice); 5 return ( 6 <div> 7 <button onClick={() => localstorageCounter.increment()}>Increment counter</button> {/* without payload */} 8 <button onClick={() => localstorageCounter.incrementWithPayload(5)}>Increment counter with payload</button> {/* with payload */} 9 </div> 10 ) 11};
- Outside of a React component, employ the
slice
function :
1import { counterSlice } from '@/slices/counter'; 2 3// without payload 4export function incrementCounter() { 5 counterSlice.increment(); 6}
Using in SSR application
As long as the initial server-side rendering occurs and there is no access to localStorage,
defaultServer
will be returned. Later, when the client-side rendering takes place, data will be fetched from localStorage and returned. As long asdefaultServer
exists in the config, both theuseKillua
hook and theslice
function will return a property namedisReady
.
If this value isfalse
, it means that it is server-side and localStorage is not accessible, and the returned value isdefaultServer
.
If this value is true, it means that it is client-side and localStorage isaccessible, and the fetched value is from localStorage.
- add
defaultServer
to the slice configuration :
1import { slice } from 'killua'; 2 3export const counterSlice = slice({ 4 key: 'counter', 5 defaultClient: 1 as number, 6 defaultServer: 2 // default value for server-side application 7});
- Within a React component, utilize the
useKillua
hook :
1'use client'; 2 3import { useKillua } from "killua"; 4 5export default function Component() { 6 const localstorageCounter = useKillua(counterSlice); 7 return ( 8 <div> 9 {localstorageCounter.isReady ? <p>{localstorageCounter.get()}</p> : <p>Loading...</p>} 10 </div> 11 ) 12};
- Outside of a React component, employ the
slice
function :
1import { counterSlice } from '@/slices/counter'; 2 3export function getCounter() { 4 // if is server-side ? return defaultServer : return localStorage value 5 return counterSlice.get(); 6} 7 8// with payload 9export function incrementCounterWithPayload() { 10 counterSlice.incrementWithPayload(5); 11}
Obfuscate data
As long as obsfacte is present in the slice config, your data is not protected but merely obfuscated. Avoid placing sensitive data on localStorage.
- To obfuscate localStorage data, simply add it to the slice config:
1import { slice } from 'killua'; 2 3export const counterSlice = slice({ 4 key: 'counter', 5 defaultClient: 1 as number, 6 obsfacte: true // obfuscate data in localStorage 7});
Expire timer
If the localStorage data expires at the moment when a user is on the website and the
useKillua
hook is in use, the data will be immediately removed from localStorage. Subsequently, theuseKillua
hook will update and return thedefaultClient
value.
If the user enters the site and the data has already expired, the
defaultClient
value will be returned.
- To set an expiration time for localStorage data, simply add it to the slice config :
1import { slice } from 'killua'; 2 3export const counterSlice = slice({ 4 key: 'counter', 5 defaultClient: 1 as number, 6 expire: '1d-9h-24m-10s' // expire time for localStorage data (1 day, 9 hours, 24 minutes, 10 seconds) 7});
Schema validation
If
schema
exists in the configuration, when the localStorage is updated usingset
orreducers
, the data will be validated against thatschema
. If it is not valid, it will not be set on the localStorage. Ifschema
is exists in the configuration, when the localStorage data is retrieved usingselectors
orget
, and if it is validated against theschema
and found to be invalid, thedefaultClient
value will be returned instead.
- To set a schema for localStorage data, simply add it to the slice config :
1import { slice } from 'killua'; 2 3export const counterSlice = slice({ 4 key: 'counter', 5 defaultClient: 1 as number, 6 schema: z.number().min(0).max(10) // zod schema or yup schema 7});
No vulnerabilities found.
No security vulnerabilities found.