redux, minus the boilerplate
immutable.js and React.js)
(for use withWhy?
Because redux is amazing, but the verbosity (const definitions, switch statements in primary reducers, etc) and fragmentation of the redux definitions can be painful to implement. This module adds a heap of magic with just enough flexibility to be useful. It basically just removes the repetitive parts and simplifies the cutting and pasting.
Installation
npm install redux-registry
Dependencies (only if using in React)
npm install --save react-redux redux
Usage
The basic steps are as follows:
- Create registers. The namespace included will be the name of the state branch in your redux store once registered
let register = 'todos'
- Set initial state and add definitions to register. These include a
name
(name of action),reduce
function and optionally a creator (simple creator functions that output something like { type: 'todos:addTodo', value: 'text' } will be automatically created). No "type" declaration necessary (this is why reducers are paired in the definition)!
register
- Create a registry (ReduxRegistry class)
let registry =
- Add registers to the registry
registry
- Create/Reduce functions through the registry. The registry internally namespaces and pairs everything to ensure proper reduction of actions. No switch statements, const definitions, etc are necessary.
let action = registry'addTodo''go to the store' // assumes a state from somewhere, usually passed in from a redux store state = registry // example state after execution: // { todos: ['go to the store'] }
- Wire up to redux
// import ReduxRegistry and extract reducers from shared instancelet reducers = ReduxRegistry // create redux state store with default state of Map()const appReducer = // define root reducerconst rootReducer = // create redux storeconst store = // use store like you normally would (e.g. in Provider)ReactDOM
- [OPTIONAL] - The ReduxRegistry class includes a "connect" method (similar signature to react-redux) that saves a lot of hassle in wiring up props/action creators to components. This is exported as a named const "connect" from the core module (which default exports a shared ReduxRegistry instance). In order to use this added magic, I require that you register the "connect" function from react-redux and "bindActionCreators" from react (the exported connect function uses these internally).
registry.js
// internally sets "connect" function // internally sets "bindActionCreators" function // continue adding registers (shown above)
Then in a component:
const App = <div className="app"> <div>User: username</div> <div>Age: userage can pull entire state branches or named nodes if using immutable</div> <button onClick=logoutAction>Logout fires action dispatcher</button> </div> props: username: 'user.name' user: 'user' dispatchers: 'logoutAction': 'user.logout' App
Contributing
- Fork the library and start by running:
npm run test:watch
- Please submit PRs with full test coverage, additions to README, etc.
- Issues will be addressed, but PRs with corrections are preferred. If submitting a PR, please attempt to follow my coding syntax/style.
Changelog
- v1.1.10 - Added more detail error messaging when incorrectly binding actions (branch not found)