redux-define-types
Define redux types in more easy way.
How to define types in traditional way?
First:
// Group 1const FETCH_DATA = 'fetch data prepare';const FETCH_DATA_SUCCESS = 'fetch data success';const FETCH_DATA_FAIL = 'fetch data fail'; // Group 2const CREATE_DATA = 'create data prepare';const CREATE_DATA_SUCCESS = 'create data success';const CREATE_DATA_FAIL = 'create data fail'; // Group 3const SET_LOCAL_CLOCK = 'set local clock'; // Group 4const SET_LOCAL_STORAGE = 'set local storage';
Second:
// Group 1const FETCH_DATA = prepare: 'fetch data prepare' success: 'fetch data success' fail: 'fetch data fail'; // Group 2const CREATE_DATA = prepare: 'create data prepare' success: 'create data success' fail: 'create data fail'; // Group 3const SET_LOCAL_CLOCK = 'set local clock'; // Group 4const SET_LOCAL_STORAGE = 'set local storage';
But what I say: Your are wasting life.
Exactly, you just repeat your defination every time. The work will make you crazy.
How to define in modern way?
; // Group 1const FETCH_DATA = ACTION_TYPES; // Group 2const CREATE_DATA = ACTION_TYPES; // Group 3const SET_LOCAL_CLOCK = ACTION_SINGLE_TYPE; // Group 4const SET_LOCAL_STORAGE = ACTION_SINGLE_TYPE; /*The same as (After compile): export const FETCH_DATA = { prepare: 'fetch data prepare', success: 'fetch data success', fail: 'fetch data fail',}; export const CREATE_DATA = { prepare: 'create data prepare', success: 'create data success', fail: 'create data fail',}; export const SET_LOCAL_CLOCK = 'set local clock'; export const SET_LOCAL_STORAGE = 'set local storage';*/
Cool, No repeat any more. And the IDE can track your variable easily, because that ACTION_TYPES
is an object:
const ACTION_TYPES = prepare: '' success: '' fail: '';
Installation
Support | Package | How to install |
---|---|---|
Babel 6 | 1.x | yarn add redux-define-types@1.1.0 |
Babel 7 | Current | yarn add redux-define-types |
npm
is also supported.
Do not forget to add babel plugin
Options
filePartner
Including the path where you define types. It can make babel transform faster.
fileString
Including the path where you define types. It can make babel transform faster.
It can be filename like: "actionTypes.js"
Or a folder like: "/action-types/"
typePrefix
Force the beginning of types as specify string. Otherwise, error will be thrown.
// x Incorrect inputconst FETCH_DATA = ACTION_TYPES; // √ Correct inputconst TYPE_FETCH_DATA = ACTION_TYPES;
typeSuffix
Force the end of types as specify string. Otherwise, error will be thrown.
// x Incorrect inputconst FETCH_DATA = ACTION_TYPES; // √ Correct inputconst FETCH_DATA_TYPE = ACTION_TYPES;
Tests
-
Clone this repository
-
Run
yarn install
-
Run
yarn run test
, and look at the file demo/transform.js