UNPKG

react-constore

Version:

Lightweight and efficient state management for React with TypeScript support

185 lines (142 loc) • 5.62 kB
# React ConStore ⚔ **Ultra-lightweight state management for React 18+ applications (~950B gzipped)** ## šŸ“Š Quick Stats | šŸ“¦ NPM Package | šŸ“ Bundle Size | ⭐ GitHub Stars | šŸ“„ Downloads | |----------------|----------------|-----------------|--------------| | [react-constore](https://www.npmjs.com/package/react-constore) | **~950B gzipped** | [⭐ Star us!](https://github.com/mostafarastegar/react-constore/stargazers) | [šŸ“ˆ NPM Trends](https://npmtrends.com/react-constore) | ## āœ… Features - āœ… **TypeScript Ready** - Full type safety out of the box - āœ… **React 18+ Compatible** - Concurrent features supported - āœ… **Zero Dependencies** - No external packages needed - āœ… **Devtools** - use [`Redux-Devtools`](docs/devtools-middleware.md) for debugging - āœ… **SSR Ready** - Works with Next.js App Router - āœ… **Tree Shakeable** - Only bundle what you use - āœ… **MIT Licensed** - Free for commercial use ## šŸ”— Quick Links - šŸ“– **Documentation**: [GitHub Repository](https://github.com/mostafarastegar/react-constore) - šŸ“¦ **Install**: `npm install react-constore` - šŸ“Š **Bundle Analysis**: [Bundle Phobia](https://bundlephobia.com/package/react-constore) - šŸ“ˆ **Download Stats**: [NPM Package](https://www.npmjs.com/package/react-constore) - šŸ› **Issues & Support**: [GitHub Issues](https://github.com/mostafarastegar/react-constore/issues) ## šŸš€ Why React ConStore? ```jsx // Traditional React state management āŒ const [count, setCount] = useState(0); const [user, setUser] = useState({ name: 'Guest' }); const [todos, setTodos] = useState([]); // Props drilling nightmare... 😱 // With React ConStore āœ… const store = createStore({ count: 0, user: { name: 'Guest' }, todos: [] }); // Use anywhere, anytime šŸŽ‰ const [count, setCount] = store.useStoreKey('count'); ``` ## ✨ Key Features | Feature | Description | Benefit | |---------|-------------|---------| | 🪶 **Ultra Lightweight** | Less than 1KB gzipped | Faster bundle, better performance | | āš›ļø **React 18/19 Ready** | Concurrent features, automatic batching | Future-proof your app | | šŸŽÆ **Smart Re-renders** | Deep equality checks | Only render when data actually changes | | šŸ” **Selective Subscriptions** | Subscribe to specific keys | Granular control over updates | | šŸ“ **Full TypeScript** | Complete type safety | Catch bugs at compile time | | 🌐 **SSR Compatible** | Next.js 13+ App Router | Server-side rendering support | ## šŸ“¦ Installation ```bash # npm npm install react-constore # yarn yarn add react-constore # pnpm pnpm add react-constore ``` ## šŸŽÆ Quick Start ```jsx 'use client'; // for Next.js App Router import createStore from 'react-constore'; // 1. Create your store const store = createStore({ count: 0, user: { name: 'Guest', age: 25 }, todos: [], theme: 'light' }); // 2. Use in components function Counter() { const [count, setCount] = store.useStoreKey('count'); return ( <div> <h2>Count: {count}</h2> <button onClick={() => setCount(count + 1)}> Increment </button> <button onClick={() => setCount(c => c + 1)}> Functional Update </button> </div> ); } function UserProfile() { const [user, setUser] = store.useStoreKey('user'); return ( <div> <h2>{user.name} (Age: {user.age})</h2> <button onClick={() => setUser({ ...user, age: user.age + 1 })}> Happy Birthday! šŸŽ‰ </button> </div> ); } // 3. Components automatically sync! function App() { return ( <div> <Counter /> <UserProfile /> </div> ); } ``` ## šŸ“š Documentation ### šŸš€ Getting Started - [Installation & Quick Start](#-installation) - [Core Concepts](docs/hooks-guide.md) ### šŸ“– API Reference - [Hooks Guide](docs/hooks-guide.md) - `useStoreKey()`, `useStoreKeys()`, `useStore()` - [Store API](docs/store-api.md) - `getState()`, `setState()`, `subscribe()` ### šŸ”§ Advanced Usage - [DevTools](docs/devtools-middleware.md) - Debugger mode and built-in middlewares - [Advanced Patterns](docs/advanced-patterns.md) - Computed values, actions, middleware - [React Integration](docs/react-integration.md) - React 18+, Next.js, React Native ### šŸ”„ Migration & Comparison - [Migration Guide](docs/migration-guide.md) - From Redux, Zustand, React Context API ### šŸ“š Best Practices & Help - [Best Practices](docs/best-practices.md) - [Testing](docs/testing.md) - [Troubleshooting](docs/troubleshooting.md) ## šŸ“Š Bundle Size Comparison | Library | Size (gzipped) | Features | |---------|----------------|----------| | **React ConStore** | **~950B** | āœ… Hooks āœ… TypeScript āœ… SSR āœ… DevTools āœ… Middleware āœ… Persist | | Redux Toolkit | ~3KB | āœ… DevTools āœ… Middleware āœ… Time Travel āŒ Simple API | | Zustand | ~600B | āœ… Simple āœ… Middleware āŒ Built-in TypeScript | | Jotai | ~4KB | āœ… Atomic āœ… TypeScript āŒ Learning Curve | ## šŸ†š When to Use **Use React ConStore when:** - āœ… You want minimal bundle size - āœ… Simple to medium complexity apps - āœ… TypeScript-first development - āœ… React 18+ features are important - āœ… Devtools for debugging - āœ… Custom middleware **Consider alternatives when:** - šŸ¤” **Redux Toolkit**: Need time-travel debugging, complex middleware - šŸ¤” **Zustand**: Need advanced features (immer, persist, devtools) - šŸ¤” **Jotai**: Bottom-up atomic state management - šŸ¤” **Context API**: Very simple, component-local state ## šŸ“„ License MIT Ā© [Mostafa Rastegar](https://github.com/mostafarastegar) --- **Made with ā¤ļø for the React community**