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
Markdown
# use-s-react
<p align="left">
<img src="https://use-s-react.christbm.dev/logo.png" width="200" alt="useS Logo" />
</p>
[](https://www.npmjs.com/package/use-s-react)
[](https://www.npmjs.com/package/use-s-react)
[](https://github.com/starts/ChristBM/use-s)
[](./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