flizk-state
v1.0.4
Published
flizkState is a simple state management utility designed to manage and persist application state using sessionStorage. It allows you to subscribe to changes in specific state keys and react accordingly. This README provides an overview of the flizkState A
Downloads
7
Maintainers
Readme
Flizk State Management
flizkState is a simple state management utility designed to manage and persist application state using sessionStorage. It allows you to subscribe to changes in specific state keys and react accordingly. This README provides an overview of the flizkState API and usage. Table of Contents
Installation
Usage
API
initialize
getState
setState
subscribe
unsubscribe
Example
Installation
bash
Using npm
npm install flizk-state
Using yarn
yarn add flizk-state
To initialise state management
const flizkState = require('flizk-state');
API initialize
This method initializes the state by loading it from sessionStorage if it exists. getState
const value = flizkState.getState(key);
This method returns the current value of the specified state key. setState
flizkState.setState(key, value);
This method sets the value of the specified state key and notifies all subscribed listeners. It also saves the updated state to sessionStorage. subscribe
flizkState.subscribe(key, listener);
This method subscribes a listener function to changes in the specified state key. unsubscribe
flizkState.unsubscribe(key, listener);
This method unsubscribes a listener function from changes in the specified state key.
Example
// Initialize flizkState
flizkState.initialize();
// Subscribe to changes in 'exampleKey'
flizkState.subscribe('exampleKey', (value) => {
console.log(`'exampleKey' changed to: ${value}`);
});
// Set the value of 'exampleKey'
flizkState.setState('exampleKey', 'newValue');
In this example, the state is initialized, a listener is subscribed to changes in the 'exampleKey', and then the value of 'exampleKey' is updated. The subscribed listener will be notified and print the new value to the console.
Feel free to customize and integrate flizkState into your application for simple and efficient state management.