UNPKG

use-s-react

Version:

useS is a minimal yet powerful React hook for managing both local and global state โ€” with zero boilerplate

208 lines (131 loc) โ€ข 4.46 kB
# use-s-react <p align="left"> <img src="https://use-s-react.christbm.dev/logo.png" width="200" alt="useS Logo" /> </p> [![npm version](https://img.shields.io/npm/v/use-s-react?color=blue)](https://www.npmjs.com/package/use-s-react) [![npm downloads](https://img.shields.io/npm/dm/use-s-react.svg)](https://www.npmjs.com/package/use-s-react) [![stars](https://badgen.net/github/stars/ChristBM/use-s)](https://github.com/starts/ChristBM/use-s) [![MIT license](https://img.shields.io/npm/l/use-s-react.svg)](./LICENSE) ## What is useS? Is a minimal yet powerful React hook for managing both **local** and **global** state โ€” with zero boilerplate. - ๐Ÿง  **Feels like** `useState`, so it's instantly familiar. - ๐Ÿšซ **No Providers**. No Context. No extra setup. - โšก **Scales globally** without wrappers or nested trees. - ๐Ÿงฉ Works with any state shape: primitives, arrays, objects, deeply nested structures. - ๐Ÿ” Supports `setState(prev => ...)` logic. - ๐Ÿงผ Built with TypeScript and powered by [`useSyncExternalStore`](https://react.dev/reference/react/useSyncExternalStore) and [`full-copy`](https://www.npmjs.com/package/full-copy) for deep reactivity. It's a **native and lightweight alternative** to Zustand, Redux Toolkit, React Context, React useReducer and even `useState` itself โ€” perfect for projects that need power and simplicity without the overhead. --- ๐Ÿ“˜ **Want to understand `use-s-react` in depth?** ๐Ÿ‘‰ [Visit the useS Documentation](https://use-s-react.christbm.dev) ## ๐Ÿ“ฆ Installation Install via npm or your preferred package manager: ```bash npm i use-s-react ``` ## ๐Ÿš€ Quick Start ### ๐Ÿ”ธ Import the hook ```tsx import { useS } from "use-s-react"; ``` ### ๐Ÿ”ธ Local state (same as `useState`) ```tsx const [count, setCount] = useS(0); ``` ### ๐Ÿ”ธ Global state (via config object) ```tsx const [count, setCount] = useS({ value: 0, key: 'global-counter' }); ``` ### โœ… Best Practice: External Global Store Use a `store.ts` to centralize your global state configs: ```tsx // store.ts export const store = { globalCounter: { value: 0, key: 'global-counter', }, globalUser: { value: { name: "John", age: 30, }, key: 'global-user', } }; // Then import and use it: import { store } from "./store"; const [count, setCount] = useS(store.globalCounter); ``` ## โ™ป๏ธ Sharing Global State Between Components ### ๐Ÿ”ธ ComponentA.tsx ```tsx import { useS } from "use-s-react"; export function ComponentA() { const [count, setCount] = useS({ value: 0, key: 'global-counter' }); return ( <div> <h3>Component A</h3> <p>Count: {count}</p> <button onClick={() => setCount(prev => prev + 1)}>Increment</button> </div> ); } ``` ### ๐Ÿ”ธ ComponentB.tsx ```tsx import { useS } from "use-s-react"; export function ComponentB() { const [count] = useS({ value: 0, key: 'global-counter' }); return ( <div> <h3>Component B</h3> <p>Count from A: {count}</p> </div> ); } ``` ## ๐Ÿ” Deep Updates & More - Updates are **deep-merged** using [`full-copy`](https://www.npmjs.com/package/full-copy), preserving nested structures: ```tsx setUser({ info: { lang: "es" } }); // doesn't erase other info keys ``` - You can also return a new state based on the previous: ```tsx setUser(prev => ({ info: { lang: prev === 'en' ? 'es' : 'en', }, }) ); ``` - Destructuring deeply ```tsx const [{ name, age }, setUser] = useS({ value: { name: "John", age: 20 }, key: "global-user" }); ``` - Infer state typing based on initial value ## ๐Ÿงช Debugging (Optional) Use `debugGlobalStore()` to inspect all global state in the console: ```tsx import { debugGlobalStore } from "use-s-react"; useEffect(() => { debugGlobalStore(); // logs all keys debugGlobalStore({ filterKey: "global-user" }); // only "global-user" debugGlobalStore({ withConsoleTable: false }); // plain logs }, []); ``` โœ… Fully compatible with React Native โ€” debugGlobalStore() gracefully falls back to console.log when console.table is not available. ๐Ÿ” console.table will be used when possible for better clarity. ## ๐Ÿ”ง API Summary `useS(initialValue: T)` Creates a local state, just like useState (but with super powers). `useS({ value, key })` Makes the state globally available for use by other components. The key must be unique. ## ๐Ÿ“œ License [MIT](./LICENSE) ยฉ ChristBM