redux-define
Installation
with npm:
npm install --save redux-define
or yarn:
yarn add redux-define
If you don’t use npm, you may grab the latest UMD build from unpkg
(either a development or a production build). The UMD build exports a
global called window.ReduxDefine
if you add it to your page via a <script>
tag.
We don’t recommend UMD builds for any serious application, as most of the libraries complementary to Redux are only available on npm.
Usage
defineAction(type, ?[subactions], ?namespace)
;
Create a redux action type with one or more subactions:
const CREATE_TODO = ; // result:console; // CREATE_TODOconsole; // CREATE_TODO_ERRORconsole; // CREATE_TODO_SUCCESS;
Namespaces can be used to separate actions through out modules and apps.
const CREATE_TODO = ; // result:console; // my-app/CREATE_TODOconsole; // my-app/CREATE_TODO_ERRORconsole; // my-app/CREATE_TODO_SUCCESS;
It's also possible to give in another constant as namespace for the new one.
const todos = ;const CREATE_TODO = ; // result:console; // my-app/todos/CREATE_TODOconsole; // my-app/todos/CREATE_TODO_ERRORconsole; // my-app/todos/CREATE_TODO_SUCCESS;
To integrate better with other redux
libraries, a special ACTION
property is
added to the constant. redux-actions
and redux-saga
for example
treat actionTypes other than string
specially.
Extra benefit of this little feature, is that it makes the separation between user actions and status updates more clear. Read more about this under best practice and integrations
const CREATE_TODO = ; // result:console; // CREATE_TODOconsole; // CREATE_TODOconsole; // CREATE_TODO_ERRORconsole; // CREATE_TODO_SUCCESS;
actionType.defineAction(type, ?[subactions])
As alternative syntax, we can use the defineAction
method on defined constants.
Constants defined in this way inherit their namespace. Making the namespace
argument obsolete.
const myApp = ;const todos = myApp;const CREATE = todos;
This is the same as writing:
const myApp = ;const todos = ;const CREATE = todos;
Or if you only need the CREATE
constant:
const CREATE = todos;
Result in these cases is the same. Except in the third case, where we only defined
the CREATE
constant:
console; // my-app console; // my-app/todosconsole; // my-app/todos_LOADINGconsole; // my-app/todos_SUCCESS console; // my-app/todos/CREATEconsole; // my-app/todos/CREATE_ERROR;console; // my-app/todos/CREATE_SUCCESS;
Best practice
Extract general state constants into a separate file so they can easily be imported and shared across different modules:
// stateConstants.jsconst LOADING = 'LOADING';const ERROR = 'ERROR';const SUCCESS = 'SUCCESS';
// app.jsconst myApp = ;
In the module; we can import the stateConstants
and optionally parent modules
to construct a namespace.
// todos.js;;; const todos = ;const CREATE = ; // result:console; // my-app console; // my-app/todosconsole; // my-app/todos_LOADINGconsole; // my-app/todos_SUCCESS console; // my-app/todos/CREATEconsole; // my-app/todos/CREATEconsole; // my-app/todos/CREATE_ERRORconsole; // my-app/todos/CREATE_SUCCESS
Use the ACTION
constant in dispatch
and in saga watchers
. This makes it
clear that an user or system ACTION
is being handled. All other subtypes
should be status
updates. They should be handled trough thunks
or sagas
,
but never dispatched by a user. Although it is possible to handle user actions
in the reducer directly, the advice is to not do this. Keep clear separation
between user actions and reducer actions.
Implementation example
stateConstants.js
const CANCELLED = 'CANCELLED';const ERROR = 'ERROR';const PENDING = 'PENDING';const SUCCESS = 'SUCCESS';
actionTypes.js
;; const DELETE_COMMENT = ;
actions.js
;; const deleteComment = ;
reducer.js
;; const initialState = isDeleting: false; const reducer = ;
sagas.js
;;; { try ; const data = ; ; catch error ; }
watchers.js
;; ;; { ;} { ;}
redux-define
?
Why use This library reduces a lot of the boilerplate that comes with defining redux
action types. This library is created as solution to organizing large ducks
Let's show the difference here. See above for a full implementation example.
When using ducks
, some of the files in the example above should be joined into
a single duck file.
Without using redux-define
const CREATE_TODO = 'CREATE_TODO';const CREATE_TODO_PENDING = 'CREATE_TODO_PENDING';const CREATE_TODO_ERROR = 'CREATE_TODO_ERROR';const CREATE_TODO_SUCCESS = 'CREATE_TODO_SUCCESS'; const DELETE_TODO = 'DELETE_TODO';const DELETE_TODO_PENDING = 'DELETE_TODO_PENDING';const DELETE_TODO_CANCELLED = 'DELETE_TODO_CANCELLED';const DELETE_TODO_ERROR = 'DELETE_TODO_ERROR';const DELETE_TODO_SUCCESS = 'DELETE_TODO_SUCCESS';
With redux-define
;; const CREATE_TODO = ;const DELETE_TODO = ;
Integrations
Created constants can be directly used in sagas
reducers
, or together
with redux-actions
.
See implementation example in this readme for implementation
details. We handle redux-actions
in actions.js and
reducer.js and redux-saga
in watchers.js
and sagas.js.