Reducify
Make Redux reducers with less effort.
http://reducify.mediadrake.com/
Installation
% npm install reducify
Usage
;; const store = ; store; // State is: 10 store; // State is: 5
Turn configurations into reducer functions
Tired of massive, unwieldly switch statements? Wish you could break up reducers into re-usable and configurable parts?
Yes, this problem is literally ruining my life.
We thought so. With Reducify you can create reducers with a configurations and sleep a bit easier.
Reducers Made Three Ways
Functions
The vanilla approach. Passing in a function just spits out the same function.
If you've got your reducers all squared away we don't want to rock the boat.
{ } ;
Configs
If you pass in a config, we'll turn it into a reducer function.
Check out the config api reference to see what you can add.
const myConfig = defaultsTo: 10 { // state will be 10 // reducer stuff }; ;
Arrays
Passing in an array is just a short version of the config above.
const myArrayConfig = 10 { // state will be 10 // reducer stuff }; ;
Arrays are deconstructed with the following signature:
[defaultsTo, [select, merge], reducerAndActions]
Some examples:
// No Select 10 myReducerFunction // Same as:defaultsTo: 10 reducer: myReducerFunction // Key Select myCount: 10 'myCount' myReducerFunction // Same as:defaultsTo: myCount: 10 select: 'myCount' reducer: myReducerFunction // Long form select myCount: 10 statemyCount {...state myCount: result} myReducerFunction // Same as: defaultsTo: myCount: 10 statemyCount {...state myCount: result} reducer: myReducerFunction
Configuration Sugar
Because we're opening the door on configuration, we get the ability to add in some user-directed magic that solves common redux boilerplate.
Action Methods
This might bring out some pitchforks, but you don't need to do a switch statement for everything. If you pass in an action type that is a method, we'll run them before we run any declared reducers.
;; const store = ; store; // State is 1 store; // State is 0
The following is the same as above
;; const store = ; // same as above
Nice! We went from 10 lines to 4. Not bad.
Keep in mind, this is still Redux. So don't take any shortcuts like trying to not make copies of your objects.
// GOODconst store = ; // BADconst store = ;
You can even combine these methods with a reducer function! The actions will always run first.
;; const store = ;
get it, you won't chase us down with pitchforks because we're letting you use switch statements too? Nevermind, sigh - moving on
Selectors
Ever dealt with mutating a large redux object? It's not a lot of fun to try and peak at your model over a massive switch statement and just hope you're getting it right.
String selectors
It's even less fun to have to deal with updating your model because Brad in product design thinks that we should go from having 1 user profile picture to 10.
You're a jerk Brad. I ate all of the spaghetti you brought in for lunch - it was only ok.
;; const store = ; store; // State is: {username: 'Brad', hasSpaghetti: false}
In the example above, we passed a string to the select
method. The string is mapped to an object key that we automatically merge and select from.
Selector Methods
You can pass in select and merge methods. This would be identical to the reducer above:
;; const store = ; store; // State is: {username: 'Brad', hasSpaghetti: false}
And you can use some aliases - $
for select and _
for merge.
const store = ; // eating Brad's spaghetti const store = ; // still eating Brad's spaghetti
Deep selectors
If you're trying to access an object that's nested into your state, you can pass in an array and we'll traverse that path for you
;; const store = ; store; // State is: {username: 'Brad', lunch: {hasSpaghetti: false}}
Action Partials
When you're declaring your reducer, you've got a chance to set some default values for all actions that go through it.
;; { } const store = ; store; // State is: 2 store; // State is: 0
Defaults
Just use the config option defaultsTo
.
;; const store = ; store; // State is: {myNumber: 30} store; // State is: {myNumber: 25}
You will get a state with all of your reducers, so if you're relying on method signature defaults, that will get overridden.
const store = ;
Statics
A cousin of defaultsTo
. Static reducers just return the state or default value regardless of action type.
;; const store = ; store; // State is: {foo: 'bar'} store; // State is: {foo: 'bar'}
Pass in a plain object or value and that's what you'll get back every time. Good for mocking and some plugins.
Credits
Reducify is free software under the MIT license. It was created in sunny Santa Monica by Matthew Drake.