React state manager inspired from redux using rxjs
MIT License
A powerful and flexible state management library developed using RxJS, inspired by the Flux architecture. Ideal for managing complex state in React applications.
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.
Initialize the state manager to manage your application's state effectively.
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.
Learn how to use the hook to consume and update state in your React components.
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>
);
}
const { count } = useGlobalState('count')
to rerender components only when specific state keys change, improving performance.onClick={counterActions.incr}
without the need for dispatchers.$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);
The useGlobalState
hook can take multiple arguments for accessing various state keys:
const { count, person } = useGlobalState("count", "person");
console.log({
count,
person,
});
This project is licensed under the MIT License.