redux-closure
v1.0.1
Published
A super lightweight Redux-like state manager using closures.
Maintainers
Readme
redux-closure
A super lightweight Redux-like state manager built with closures.
Perfect for learning Redux concepts or small projects where you don’t want the full Redux library.
📦 Installation
npm install redux-closure🚀 Quick Example
const { createStore } = require("redux-closure");
// Reducer function
function counterReducer(state = { count: 0 }, action) {
switch (action.type) {
case "INCREMENT":
return { count: state.count + 1 };
case "DECREMENT":
return { count: state.count - 1 };
default:
return state;
}
}
// Create store
const store = createStore(counterReducer);
// Subscribe to state changes
const unsubscribe = store.subscribe(() => {
console.log("State changed:", store.getState());
});
// Dispatch some actions
store.dispatch({ type: "INCREMENT" }); // State changed: { count: 1 }
store.dispatch({ type: "INCREMENT" }); // State changed: { count: 2 }
store.dispatch({ type: "DECREMENT" }); // State changed: { count: 1 }
// Stop listening
unsubscribe();📝 API Reference
createStore(reducer, initialState?)
Creates a new store.
- reducer:
(state, action) => newState— pure function that describes how state changes. - initialState (optional): the starting state.
store.getState()
Returns the current state.
store.dispatch(action)
Updates the state by running the reducer with the current state and the given action.
- action: an object with at least a
typeproperty (e.g.{ type: "INCREMENT" }).
store.subscribe(listener)
Registers a callback that runs whenever the state changes.
Returns an unsubscribe function to stop listening.
⚡ Why redux-closure?
- No dependencies
- Just ~20 lines of code
- Super easy to understand
- Teaches the core Redux pattern using closures
📄 License
MIT © 2025 Prince Verma
