Redux Modux
Redux Modux is concept to create the module in redux app for Create reducer, action handler and actions in a single file.
Document for version 1 see
Installation:
$ npm install redux-modux --save
$ yarn add redux-modux
Table of Contents
- Source Structure
- Create Module
- How to use a Module
- Default Action handler
- Get action type string
- Handle Other Action Type
App Structure
src
...
|-actions
|-components
|-containers
|-modules
|-moduleA
|-moduleB
|-index.js
|-store
...
Create Module
Create reducer, action handler and actions in a single file with createModule
.
Example Create profile
module.
// modules/profile/index.js ; const initialState = firstName: '' lastName: '' age: ''; const updateProfileValue = { return ...state firstName: actionfirstName lastName: actionlastName age: actionage ;}; const handlers = updateProfileValue; moduleName: 'Profile' // Optional (Prefix when console.log action's type) initialState handlers;
The output of profile
module
; console; // The log is:// {// state: reducer// updateProfileValue: action// }
How to use a module
createModule
return state
and key of actions
State
// modules/index.js ;; todo: todostate profile: profilestate;
Action
;;; Component { const newProfile = firstName: 'Peter' lastName: 'Parker' age: '25' ; thisprops; }; { return <button onClick=thishandleClickUpdateProfile>Update Profile</button>; } nullHome;
Default Action handler
set
; ;
resetDefault
Reset module state to intital state
; ;
Get action type string
You can get action type from action
; someModuleupdateProfileValueactionType; // @@reduxAction_moduleName.updateProfileValue
Handle Other Action Type
How to handle action type that is not existing type in the module.
Add handleOtherTypes
key to handlers parameter.
// modules/profile/index.js... const handlers = updateProfileValue handleOtherTypes: // Example to handle other action types state state state ...