Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 5246

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 27, 20242024-11-27T07:28:06+00:00 2024-11-27T07:28:06+00:00

Pulsy Readme updated

  • 61k

Pulsy – A Lightweight State Management Library for React

Pulsy is a lightweight, flexible, and easy-to-use state management library for React that provides features such as persistence, middleware, memoization, computed and composed stores, time travel, and DevTools integration. It helps you efficiently manage global state in React applications without unnecessary complexity.

Features

  • Global State Management: Manage state across components using a simple store API.
  • Persistence: Automatically persist store data in localStorage or custom storage solutions.
  • Middleware: Modify and handle store updates through middleware functions.
  • Memoization: Avoid unnecessary renders by using memoized state values.
  • Computed Stores: Derive and calculate state from existing stores.
  • Composable Stores: Combine multiple stores into one store for modular state management.
  • Time-Travel: Rewind and forward state changes.
  • DevTools Integration: Track and debug state updates in development mode.

Installation

npm install pulsy # or yarn add pulsy 
Enter fullscreen mode Exit fullscreen mode


Basic Usage

Step 1: Configure Pulsy

Pulsy can be configured globally to enable DevTools, default memoization, persistence, and callback hooks for store creation and updates.

import { configurePulsy } from 'pulsy';  configurePulsy({   enableDevTools: process.env.NODE_ENV === 'development',   persist: true, // Globally enable persistence by default   defaultMemoize: true, // Enable memoization for all stores by default   onStoreCreate: (name, initialValue) => console.log(`Store "${name}" created! Initial value:`, initialValue),   onStoreUpdate: (name, newValue) => console.log(`Store "${name}" updated! New value:`, newValue), }); 
Enter fullscreen mode Exit fullscreen mode

Step 2: Create a Store

To create a store, use the createStore function. A store holds the global state and can be used anywhere in your React application.

import { createStore } from 'pulsy';  // Create a store named 'counter' with an initial value of 0 createStore('counter', 0); 
Enter fullscreen mode Exit fullscreen mode

Step 3: Use the Store in a Component

Pulsy provides the usePulsy hook to access and update the store’s value in your React components. Let’s create a counter component:

import usePulsy from 'pulsy';  function CounterComponent() {   const [count, setCount] = usePulsy<number>('counter');    const increment = () => setCount((prev) => prev + 1);    return (     <div>       <p>Current Count: {count}</p>       <button onClick={increment}>Increment</button>     </div>   ); }  export default CounterComponent; 
Enter fullscreen mode Exit fullscreen mode


Persistence

Pulsy makes it easy to persist store values in localStorage or any other custom storage system. Simply pass the persist option when creating the store.

createStore('counter', 0, { persist: true }); 
Enter fullscreen mode Exit fullscreen mode

The value of the counter store will now persist across page reloads.

Example: Using Custom Storage

You can also configure Pulsy to use custom storage, such as sessionStorage or any other storage engine that implements the Storage interface:

createStore('sessionCounter', 0, {   persist: {     storage: sessionStorage, // Use sessionStorage instead of localStorage     serialize: (value) => JSON.stringify(value),     deserialize: (value) => JSON.parse(value),   }, }); 
Enter fullscreen mode Exit fullscreen mode

This will store sessionCounter in sessionStorage.


Middleware

Middleware allows you to intercept and modify store updates before they are committed. You can add middleware when creating a store, or later using addMiddleware.

const loggingMiddleware = (nextValue, prevValue, storeName) => {   console.log(`[${storeName}] changed from ${prevValue} to ${nextValue}`);   return nextValue; };  createStore('counter', 0, { middleware: [loggingMiddleware] }); 
Enter fullscreen mode Exit fullscreen mode

In this example, the middleware logs each state change in the counter store.

Example: Asynchronous Middleware

Pulsy supports asynchronous middleware for handling asynchronous tasks like API calls:

const asyncMiddleware = async (nextValue, prevValue, storeName) => {   console.log(`Fetching data before updating ${storeName}...`);   const data = await fetch('https://api.example.com/data').then((res) => res.json());   return nextValue + data.amount; };  createStore('counter', 0, { middleware: [asyncMiddleware] }); 
Enter fullscreen mode Exit fullscreen mode

In this example, the middleware fetches some data from an API before updating the store.


Time-Travel State Management

Pulsy allows you to manage state history using the useTimeTravel hook, giving you the ability to undo and redo state changes.

import { useTimeTravel } from 'pulsy';  function TimeTravelCounter() {   const [count, setCount, undo, redo] = useTimeTravel<number>('counter');    return (     <div>       <p>Count: {count}</p>       <button onClick={() => setCount(count + 1)}>Increment</button>       <button onClick={undo}>Undo</button>       <button onClick={redo}>Redo</button>     </div>   ); } 
Enter fullscreen mode Exit fullscreen mode

Example: Display State History

You can access the full history of state changes using the historyRef provided by useTimeTravel:

function HistoryCounter() {   const [count, setCount, undo, redo, history] = useTimeTravel<number>('counter');    return (     <div>       <p>Count: {count}</p>       <button onClick={() => setCount(count + 1)}>Increment</button>       <button onClick={undo}>Undo</button>       <button onClick={redo}>Redo</button>       <p>History: {history.join(', ')}</p>     </div>   ); } 
Enter fullscreen mode Exit fullscreen mode


Computed Stores

Computed stores derive their state from other stores. Pulsy allows you to create stores whose values are based on one or more other stores.

import { createComputedStore } from 'pulsy';  createComputedStore('doubleCounter', () => {   const counter = getStoreValue<number>('counter');   return counter * 2; }, ['counter']); 
Enter fullscreen mode Exit fullscreen mode

Here, doubleCounter is automatically updated whenever the counter store changes.

Example: Display Computed Store in a Component

You can now access the computed store just like a regular store:

function DoubleCounterComponent() {   const [doubleCount] = usePulsy<number>('doubleCounter');    return (     <div>       <p>Double Counter: {doubleCount}</p>     </div>   ); } 
Enter fullscreen mode Exit fullscreen mode


Composing Stores

Pulsy supports composing multiple stores into a single store. This is especially useful for managing complex state by grouping related pieces of state together.

import { composeStores } from 'pulsy';  const [getComposedStore, setComposedStore] = composeStores('userProfile', {   username: 'userNameStore',   age: 'ageStore', });  const UserProfileComponent = () => {   const userProfile = getComposedStore();    return (     <div>       <p>Username: {userProfile.username}</p>       <p>Age: {userProfile.age}</p>     </div>   ); }; 
Enter fullscreen mode Exit fullscreen mode

Example: Updating Composed Stores

You can also update specific parts of a composed store using the setComposedStore function:

setComposedStore({   username: 'newUsername', }); 
Enter fullscreen mode Exit fullscreen mode


Namespaced Stores

Pulsy allows you to create namespaced stores to keep related stores organized and avoid naming collisions in large applications.

import { createNamespacedStore } from 'pulsy';  // Create a namespaced store for user-related data const useUserStore = createNamespacedStore('user');  function UserComponent() {   const [username, setUsername] = useUserStore<string>('username');    return (     <div>       <p>Username: {username}</p>       <button onClick={() => setUsername('newUsername')}>Update Username</button>     </div>   ); } 
Enter fullscreen mode Exit fullscreen mode

Here, all user-related stores (e.g., user:username, user:age) are grouped under the user namespace.


DevTools Integration

Pulsy integrates with browser DevTools to help track and debug store updates. When DevTools are enabled, you’ll see logs about store updates, state changes, and performance measurements in your console.

configurePulsy({   enableDevTools: true, // Logs detailed store activity to the console }); 
Enter fullscreen mode Exit fullscreen mode

Pulsy logs helpful information such as when a store is created or updated, middleware execution, and time-travel actions in development mode.


Full Example: Managing User Profile with Persistence and Middleware

Let’s combine multiple Pulsy features into a single example.

import { createStore, usePulsy, configurePulsy } from 'pulsy';  // Global configuration configurePulsy({   enableDevTools: true,   persist: true, });  // Middleware to log store updates const loggingMiddleware = (nextValue, prevValue, storeName) => {   console.log(`Store ${storeName}   updated from ${prevValue} to ${nextValue}`);   return nextValue; };  // Create a store for user profile createStore('userProfile', {   username: 'guest',   age: 25, }, { middleware: [loggingMiddleware], persist: true });  // Component to manage user profile function UserProfileComponent() {   const [userProfile, setUserProfile] = usePulsy('userProfile');    const updateUsername = () => {     setUserProfile((prevProfile) => ({       ...prevProfile,       username: 'newUsername',     }));   };    return (     <div>       <p>Username: {userProfile.username}</p>       <p>Age: {userProfile.age}</p>       <button onClick={updateUsername}>Change Username</button>     </div>   ); }  export default UserProfileComponent; 
Enter fullscreen mode Exit fullscreen mode

In this example, the userProfile store is persisted, logged by middleware, and accessible via the usePulsy hook. The UserProfileComponent displays and updates the store in a simple UI.


Conclusion

Pulsy is a powerful and flexible state management library for React that provides out-of-the-box support for persistence, middleware, computed stores, time-travel, and DevTools. Its simple API and wide range of features make it suitable for both small and large-scale applications.

frontendjavascriptprogrammingwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.