This is a minimalist state management library developed based on React. It uses only 100 lines of code to implement synchronous state updates and value transfer across components. The package size is only 7kb. You can use it in your project with confidence without any performance loss!
Benefit
- The status sharing is convenient and suitable for small and medium-sized projects.
- Minimal boilerplate code, improving development experience.
- Data persistence avoids loss of state when refreshing the page.
- High flexibility to meet different business needs.
- A safe default value to prevent an error when the
keydoes not exist.
New features
- Easy to use:
createTangoStoreanduseTangoProvides a conciseAPI,Consumers can easily createStoreand subscribe to states. - Default value protection: Ensures that the default value of a state is not overwritten by an undefined value.
- State change check: Events are dispatched only when the state actually changes, reducing invalid updates.
- Error handling: Ensure that when accessing a non-existent
keyinuseTangono error is thrown, but the default value is returned.
Key functions
- Subscription status: Subscription status changes via
store.subscribe. - Respond to state changes: When the state changes, update the local state of the component, triggering the component to re-render.
- Unsubscribe: When the component is uninstalled, unsubscribe to avoid memory leaks.
Implementation details
useState: Used to maintain a local state in the component, the initial value isstore.getState()[key]。useEffect:Subscribe to state changes when the component is mounted; unsubscribe when the component is unmounted. Whenstoredispatches achangeevent,useTangowill get the latest state from the event and update the local state.- Performance optimization: Only the subscription function is triggered when the state is updated, avoiding unnecessary re-rendering and achieving efficient state updates.
Reactecosystem:useTango Hook,integratesTangoStoreinto theReactecosystem, supports automatic UI updates when state changes occur, and makes development simpler.
Workflow
- Create a
Store: Users create aTangoStoreinstance throughcreateTangoStoreand pass in the initial state. - Update State: When a user updates the state via
store.setState,TangoStoredistributesa changeevent. Note: There is no need to worry about unrelated subscribers receiving the status as well, as we set the current state to only affect components that have introduceduseTango Hook. - Subscription status: In
a Reactcomponent, the user clicks theuseTango HookSubscribe toTangoStorestate change. When the state changes,useTangoThe local state of the component is updated, triggering the component to re-render. - Unsubscribe:
useTangowhen the component is uninstalled Automatically cancel the subscription to avoid memory leaks.
Follow these steps below
1.Installation
npm i tango-store-cw2.Create
Createstore.js store.js in the catalogDescription:
createTangoStore() is a factory function that creates TangoStore instance. The user can pass in the initial state initialState, the function returns a new TangoStore instance.import { createTangoStore } from "tango-store-cw";
// Define the initialValue
export const initialValue = createTangoStore({ name: "Lucy", age: 18 });
// Define the update function
export const setName = (v) => {
initialValue.setState({ name: v });
};
export const setAge = (v) => {
initialValue.setState({ age: v });
};3.Use
Show datas on the page ADescription:
useTango Hook for use in React Change in the status of the subscribed TangoStore in the component and return the specified key corresponding to the state value.import React from "react";
import { initialValue } from "@/TangoStore/TangoStore";
import TSpace from "@/component/TSpace/index";
import { useTango } from "tango-store-cw";
const CaseA = () => {
const name = useTango(initialValue, "name");
const age = useTango(initialValue, "age");
return (
<div>
<h1>Page A</h1>
<TSpace>name in Tango:{name}</TSpace>
<TSpace>age in Tango:{age}</TSpace>
</div>
);
};
export default CaseA;
Note: Import the update functions
setName and setAge to modify the initial valuesimport React from "react";
import { setName, setAge } from "@/TangoStore/TangoStore";
import TButton from "@/component/TButton/index";
const CaseB = () => {
const ChangeValue = () => {
setName("Brown");
setAge(20);
};
return (
<div>
<h1>Page B</h1>
<TButton onClick={ChangeValue}>change the datas of Page B</TButton>
</div>
);
};
export default CaseB;

