cooldux
Just a few helpers for the redux ducks pattern
makeActionCreator
Since we're throwing actions and reducers into a single file, let's not bother with explicit constants:
const somethingStart = cooldux; // later something dispatches our action..; // and after that, our reducer can deal with action types as such: { }
resetReducer
A reducer factory function that can handle a reset action and set the created reducer back to its initial state.
const reducer = cooldux; //something dispatches a reset:;
promiseHandler
Async API calls with redux typically use 3 actions: a Start, End, and Error.
If you're using Promises, cooldux.promiseHandler
wraps this all up for you and automatically dispatches the correct actions:
const exampleStart exampleEnd exampleError exampleHandler = ; //your redux-thunk action creator { return { const promise = ; return ; };} //your reducer can use the action types returned above with promiseHandler { }
If you follow the cooldux naming conventions, you can further remove boiler plate code using the cooldux.promiseHandler
's reducer:
const exampleHandler exampleReducer = ; //your redux-thunk action creator { return { const promise = ; return ; };} //this will automatically modify state in the same way as the previous example;
combinedHandler
If you have multiple parts of the state you'd like to manage with cooldux in a single file, you can even further remove boiler plate code using the cooldux.combinedHandler
's function:
const exampleAHandler exampleBHandler initialStateCombined reducerCombined } = ; { return { const promise = ; return ; };} { return { const promise = ; return ; };} //this will run through all the reducers created;
promiseMiddleware
You can optionally add the cooldux promise-aware middleware to redux to automatically handle dispatching the different parts of an asynchronous call.
Just apply promiseMiddleware:
;const store = ;
Now this lets us shorten our previous example by not having to pass around the dispatch function normally required by redux-thunk:
const exampleAAction exampleBAction reducerCombined } = ; const exampleA = ;const exampleB = ; ;
makeDuck
Now that we're using middleware, can we make the above example even more automatic?
Of course!
In cases where you just want to provide functions and let cooldux manage the actions, dispatching, and the reducer, you can provide cooldux.makeDuck
with a map of those functions.
const duck = ; const exampleA exampleB = duck;reducerCombined;
That's your entire state handler for a couple of functions that can by synchronous or promise-returing! Your react or whatever view can just look in redux for props.someState.exampleA
or props.someState.exampleAError
.
cache
Sometimes you only want to do an action once. For example only fetch data from an API if you haven't already. makeDuck
and promiseHandler
can take an option to return actions that will check state and only call your action if needed.
const duck = ; const exampleA exampleACached = duck;reducerCombined;
In this example if you call exampleACached
the cooldux middleware will check the state for foo.exampleA
and if it sees a truthy value return that, otherwise call the exampleA
function. Your namespace must be the same property this duck belongs to.
That's it for now!
Each of the previous examples build on eachother, you certainly don't need to use all of the cooldux functions. It might be that just the early examples feel helpful or it may be that you're on board to use it all and just let the automagic take you.