Giter VIP home page Giter VIP logo

rxdux-state-manager's Introduction

rxdux-state-manager

A powerful and flexible state management library developed using RxJS, inspired by the Flux architecture. Ideal for managing complex state in React applications.

Installation

Easily integrate rxdux-state-manager into your project:

npm install rxdux-state-manager

or

yarn add rxdux-state-manager

Tip: This library can be used as a React hook for efficient state management.

Getting Started with rxdux-state-manager

Initializing the State Manager

Initialize the state manager to manage your application's state effectively.

counter-state-manager.ts

import { easyStateManager } from "rxdux-state-manager";

export const {
  useStateManager: useGlobalState,
  $state: $globalState,
  updateState: updateGlobalState,
} = easyStateManager({
  count: 0,
  person: {
    name: "John Doe",
    age: 22,
  },
});

class CounterActions {
  incr = () => {
    updateGlobalState((draft) => {
      draft.count++;
    });
  };

  incrAge = () => {
    updateGlobalState((draft) => {
      draft.person.age++;
    });
  };
}

export const counterActions = new CounterActions();

Key Feature: When passing a function to the updater, the draft argument can be mutated freely. Changes will be made immutable and become the next state once the producer ends.

Using the Hook to Access and Update State

Learn how to use the hook to consume and update state in your React components.

Counter.tsx

import React from "react";
import { counterActions, useGlobalState } from "./counter-state-manager";

export default function Counter() {
  const { count } = useGlobalState("count");
  
  return (
    <div className="p-2 rounded border border-blue-500">
      <p className="text-purple-600">{count}</p>
      <button className="border p-1 rounded" onClick={counterActions.incr}>
        Increment
      </button>
    </div>
  );
}

Why Use rxdux-state-manager?

  1. Selective Rerendering: Use const { count } = useGlobalState('count') to rerender components only when specific state keys change, improving performance.
  2. Effortless State Updates: Simplify state updates with onClick={counterActions.incr} without the need for dispatchers.

Accessing Current State with RxJS BehaviorSubject

$globalState is an RxJS BehaviorSubject, allowing you to access the current state anywhere in your application:

const state = $globalState.value; 
console.log(state.person.age);

Using Multiple State Keys with useGlobalState

The useGlobalState hook can take multiple arguments for accessing various state keys:

const { count, person } = useGlobalState("count", "person");
console.log({
  count,
  person,
});

Advantages of Using rxdux-state-manager

  • Efficient State Management: Manage your application’s state efficiently with RxJS and React.
  • Scalable and Maintainable: Ideal for complex applications requiring a scalable and maintainable state management solution.
  • Improved Performance: Optimize performance with selective rerendering and immutable state updates.

License

This project is licensed under the MIT License.

rxdux-state-manager's People

Contributors

dhruvanwd avatar webdevelopementcenter avatar

Watchers

 avatar

Forkers

dhruvanwd

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.