How anurag6713/ninja-store works
Project type and purpose
• This is an open-source React state-management library (“ninja-store”) published on npm.
• Purpose: To let React and React Native components create and share simple named stores (with optional reducer logic) via a custom hook (useStore).
File structure
• Top-level files:
• createStore.js – implements the factory that returns useStore.
• stores.js – holds the internal registry of all named stores and implements subscribe/notify.
• index.js / index.d.ts – module entry point and TypeScript declarations.
• package.json / README.md – metadata and documentation.
• No “frontend” or “backend” dirs – this is purely a client-side library.
• No build or deployment scripts present in the tree (they’re presumably external).
Key insights from the README
• The public API surface is:
a. createStore(name, initialState)
b. createStore(name, reducer, initialState)
c. The returned object exposes { useStore }, where useStore([mapState]) returns [state, setState] or [state, dispatch].
• It supports:
• Plain setter API (setState)
• Reducer/dispatch API
• Optional mapState selector to subscribe only to slices of the store.
1. Consumer Components
– any React or React Native component that calls useStore().
2. ninja-store Library
– createStore module (createStore.js)
– Stores registry (stores.js)
– Exports (index.js / index.d.ts)
3. Store Instance
– one per unique string name, holding:
• state container (JS object)
• Of subscribers (React component hooks)
4. React (external)
– the React runtime that calls hooks and re-renders components.
1. React component calls → useStore()
2. useStore() registers the component’s subscription with a named store in “stores registry.”
3. Component uses setter or dispatch to update store state → store registry updates state.
4. Store registry notifies all subscribed components → React schedules re-render.
c. Architectural pattern
• Simple pub/sub (observer) pattern combined with React hooks.
• Optional reducer pattern for structured actions.
• No global context provider – stores are managed in an internal registry keyed by name.
d. Key technologies
• JavaScript (ES6 modules)
• React / React Native hooks API
• Optional TypeScript typings (index.d.ts)
Final recommendations
• Multiple consumer components subscribing to the same store instance to emphasize shared state.