redux-nc
v1.0.2
Published
Redux no constants =========
Downloads
20
Readme
Redux no constants
A small library for working with redux without constants
Installation
- npm:
npm i -S redux-nc
Basic usage
First you need apply promise middleware
import { promiseMiddleware } from 'redux-nc'; applyMiddleware(..., promiseMiddleware);
Create the first action creator:
import { createActions } from 'redux-nc'; export default createActions('UNIQ_ACTIONS_NAME', { test: (...args) => true, });
Create reducer:
import { createReducer, getActionIds } from 'redux-nc'; import actions from 'actions'; const types = getActionIds(actions); export default createReducer({ // Define initial store test: false, }, { [types.test]: (state, action) => action.payload, });
Create the first async action creator:
import { createActions, async } from 'redux-nc'; export default createActions('UNIQ_ACTIONS_NAME', { @async() test(...args) { return (dispatch) => { dispatch(this.some(...ANY_DATA)); return new Promise(resolve => resolve(true)); } }, some(...args) { return ...args; } });
Create reducer for async actions:
import { createReducer, getActionIds } from 'redux-nc'; import actions from 'actions'; const types = getActionIds(actions); export default createReducer({ // Define initial store test: false, }, { [types.test.begin]: (state, action) => action.payload, [types.test.success]: (state, action) => action.payload, [types.test.failed]: (state, action) => action.payload, });
Complete decorator:
import { createActions, async, complete } from 'redux-nc'; export default createActions('UNIQ_ACTIONS_NAME', { @async() /** * error: false/true - types.test.success / types.test.failed * payload: previus action result */ @complete(({ error, payload }) => (dispatch) => { if (error) { } else { } // You can also use dispatch here // dispatch(this.some(...ANY_DATA)); // NOTE: for use this, // you should pass `function` not arrow func to @complete }) test: () => new Promise(resolve => resolve(true)), some(...args) { return ...args; } });