Baguni is a Korean word for 'basket' and it's also the name of this most simple and lightweight package for managing state across React components.
Baguni's primary goal is to redefine the use of state in React, traditionally managed by the useState
hook. While useState
is limited to the scope of a single component, useBaguni
breaks these boundaries, allowing developers to share the state of a single variable across multiple components seamlessly.
In case if there is a necessity for more complex state management, preserving state and/or use of utilities/extensions, something like Zustand or Jotai could suit better, but before making a decision, you may want to read why sections first
- Simple and Lightweight: Designed as drop-in replacement for
useState
in React. - State Sharing: Effortlessly share state between components.
- React Friendly: Works out of the box with existing React/Nextjs applications.
- No Boilerplate: Reduce the need for complex state management setups.
- No learning curve: If you know
useState
, you knowuseBaguni
. - Typescript ready: Use it with javascript or typescript as you want.
- Smallest state management library: 1582 bytes (1106 without declaration file)
- No dependencies: The only dependency is
react
itself
- Simple
- Lightweight
- Easy to set up
- No need to learn syntax or API - just replace existing
useState
withuseBaguni
If you don't want to install the package, you may just copy standalone/baguni-standalone.js/.ts
to your project and use it as you like.
npm install baguni
// src/lib/variables.ts
import { createBaguni } from "baguni";
export const APP_COUNTER = createBaguni<number>(0);
// src/components/component.tsx
import { useBaguni } from "baguni";
import { APP_COUNTER } from "../lib/variables";
const Component = () => {
const [appCounter] = useBaguni(APP_COUNTER);
return <div>Counter: {appCounter}</div>;
};
export default Component;
// src/components/button.tsx
import { useBaguni } from "baguni";
import { APP_COUNTER } from "../lib/variables";
const Button = () => {
const [counter, setAppCounter] = useBaguni(APP_COUNTER);
return (
<button
onClick={() = {
setAppCounter(counter + 1);
}}
>Add 1 to counter</button>
);
};
export default Button;
// src/api/get_current_counter_value.ts
import { APP_COUNTER } from "../lib/variables";
const getCurrentCounterValue = () => {
const counter = APP_COUNTER.get();
console.log("counter value: ", counter);
};
What works for useState
, works for useBaguni
as well
export const simpleString = createBaguni<string>("");
export const simpleNumber = createBaguni<number>(0);
export const simpleArray = createBaguni<[]>([]);
Contributions are welcome! Feel free to open a pull request or an issue to discuss potential changes or improvements.
MIT License, 2024