A paper-thin, 100% typesafe Redux for babies
Install
# Using Yarn: yarn add babydux # Or, using NPM: npm install babydux --save
Use
1. Create a store
// If you're using Babydux with TypeScript, declare your store's types.
Be sure to define a key for each value in your model, even if the value is initially undefined
.
2. Connect your React components
// Update the component when `today` changes
That's all there is to it.
Features
Effects
Though Babydux automatically updates your model for you, it also lets you listen on and react to model updates (similarly to how vanilla Redux lets you subscribe to Actions). Babydux subscriptions are full Rx observables, so you have fine control over how you react to a change:
store .on'today' .filter_.getTime % 2 === 0 // only even timestamps .debounce100 .subscribeconsole.log'Date changed', _
Lensed connects
Instead of updating your React component when anything on the model changed, you subscribe just to specific properties on your model. Let's modify our React example to only update when today
changes:
Everything is the same as before, I just added 'today'
as an argument to the function returned by connect
.
Partial application all the way through
Partially apply the connect
function to yield a convenient withStore
function:
Or, partially apply the set
function to yield a convenient setter:
setUserssetUsers
Built-in logger
If you create your store with withLogger
higher order store, all model updates (which key was updated, previous value, and new value) will be logged to the console.
To enable the logger, import withLogger
and wrap your store with it:
And logs look like this:
Plugins
Babydux is easy to modify with plugins (also called "higher order stores"). Just define a function that takes a store as an argument and returns a store, adding listeners along the way. For convenience, Babydux supports 2 types of listeners for plugins:
Babydux also supports .on()
and .before()
, but because a plugin doesn't know what Actions will be bound to it, these are generally unsafe to use.
Design philosophy
Goal #1 is total type-safety.
Getting, setting, reading, and listening on model updates is 100% type-safe: use a key that isn't defined in your model or set a key to the wrong type, and you'll get a compile-time error. And connected components are just as type-safe.
Goal #2 is letting you write as little boilerplate as possible.
Babydux is like Redux, but reducers are already baked-in. Babydux automatically creates an action and a reducer for each key on your state, so you don't have to write tedious boilerplate. Babydux still emits Actions under the hood (which you can listen on to produce effects), but gives you an incredibly simple get
/set
API that covers most use cases.
If you're using Babydux with the provided React connector, Babydux will update your React component any time a reducer fires (just like React-Redux). You can optionally filter on specific state keys that you care about for more targeted updates.
Tests
yarn test
License
MIT