Ramduck Redux
Tired of creating reducers that aren't curried. Interested in partial application with Ramdajs ?
This little project brings the power of curry and composition (thanks to Ramda) into your redux project.
Previously
// reducer/user.jsconst INITIAL_STATE = name: '' email: ''; // Actions typesconst CHANGE_NAME = 'change user name';const CHANGE_EMAIL = 'change user email';const CHANGE_USER = 'change user'; // Actions creatorconst changeName = type: CHANGE_NAME name ;const changeEmail = type: CHANGE_EMAIL email ;const changeUser = type: CHANGE_USER name email ; // Reducer: { }
With Ramduck Redux
// reducer/user.js const initialState = name: '' email: ''; // Actions creatorconst changeName = ;// with obj ofconst changeEmail = ;// with 2 argumentsconst changeUser = // no need of action type, use your function as a string in order// to retrieve the type:// `${changeName}` === 'change user name' // Reducer:'user' // you must name it (handy with ramduck-react-redux) // with evolve: // with omit, merge and composition: // or with mergeAction (same behavior, it merge the action payload by removing // the type properties): // when(changeUser, mergeAction), // In your main state file: reducer/index.js // you can combine// your reducers easily with:const rootReducer = // your state will take the reducer name declare above and put it into a global// state object of the shape:// { user: <the user state> }
Installation
With npm:
npm i --save ramduck-redux
Or with yarn:
yarn add ramduck-redux
Usage
Actions
You can easily create curried action creator with
the function action, action1, action2, action3, action4, actionN
:
// action with no parameter, only a typeconst initialize = ;// action with 1 parameterconst changeName = // action with 2 parameterconst changeUser = // same thing for action2, action3, action4 :) // you can also create actions of N arguments with actionN// const veryHugeAction = actionN(8, 'some type', a => b => c => e => f => g => h => i => ...) // and then: // { type: 'MY_TYPE' } // { type: 'CHANGE_NAME', name: 'John' } // { type: 'CHANGE_USER', name: 'John', email: 'john@mail.com' } // action creators are curried:const changeJohnEmail = ; // [Function]const action = ; // { type: 'CHANGE_USER', name: 'John', email: 'john@mail.com' } // no need of action types. You can access to the type be converting your// action creator to string`` === 'CHANGE_USER' // true
Reducer
You can create reducers much more easily with combine, init, when
:
const simpleReducer = ; ; // { name: 'John' }; // { name: 'Jane' } const changeNameReducer = ; // { name: 'john' } // you can easily combine those methods to create much more complex// reducers:const reducer =
API Reference
data EmptyAction :: { type :: String } data Action a :: { type :: String | a } --| Actions helpers action :: String -> () -> EmptyAction actionN :: Number -> String -> (* -> a) -> * -> Action a action1 :: String -> (a -> b) -> a -> Action b action2 :: String -> (a -> b -> c) -> a -> b -> Action c action3 :: String -> (a -> b -> c -> d) -> a -> b -> c -> Action d action4 :: String -> (a -> b -> c -> d -> e) -> a -> b -> c -> d -> Action e mergeAction :: Action a -> Object b -> Action { | a | b } --| Reducers helpers init :: a -> (Action b, a) -> a whenAction :: String -> (Action a -> b -> b) -> (b, Action a) -> b whenActions :: [String] -> (Action a -> b -> b) -> (b, Action a) -> b when :: String|[String] -> (Action a -> b -> b) -> (b, Action a) -> b createReducer :: String -> [((b -> Action a) -> b)] -> b -> Action a -> b createRootReducer :: [((b -> Action a) -> b)] -> b -> Action a -> b