create-action-thunk
TypeScript icon, indicating that this package has built-in type declarations

1.0.18 • Public • Published

create-action-thunk

You can easly create Redux async actions in React and TypeScript - similar to https://github.com/piotrwitek/typesafe-actions

Table of Contents


Installation

$ npm i --save create-action-thunk

⇧ back to top


Tutorial

Configuring store

Install redux-thunk

$ npm i --save redux-thunk

Import thunk and configure store

import thunk from 'redux-thunk';

...

createStore(
        rootReducer,
        initialState,
        applyMiddleware(thunk, ...)
        );

Creating actions

export const somethingActions = {
    loadSomethingSuccess: createAction('LOAD_SOMETHING_SUCCESS', something => ({
        type: 'LOAD_SOMETHING_SUCCESS',
        payload: something
    })),

    loadSomething: createActionThunk('LOAD_SOMETHING', dispatch => {
        Promise.then(something => {
            dispatch(somethingActions.loadSomethingSuccess(something));
        }).catch(error => {
            throw(error);
        });
    })
};

// OPTIONAL - creating new action type used by reducer
const returnOfActions = Object.values(somethingActions).map(getReturnOfExpression);
export type SomethingActions = typeof returnOfActions[number];

Creating reducers

export default function somethingReducer(state: SomethingState = initialState.something, action: SomethingActions): SomethingState {
    switch (action.type) {
        case getType(somethingActions.loadSomethingSuccess):
            return action.payload;
        case getType(somethingActions.loadSomething):
            return state;
        default:
            return state;
    }
}

⇧ back to top

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.0.183latest

Version History

VersionDownloads (Last 7 Days)Published
1.0.183
1.0.171
1.0.161
1.0.151
1.0.141
1.0.121
1.0.111
1.0.101
1.0.91
1.0.81
1.0.7-cd1
1.0.72
1.0.61
1.0.51
1.0.41
1.0.31
1.0.21
1.0.11
1.0.01

Package Sidebar

Install

npm i create-action-thunk

Weekly Downloads

2

Version

1.0.18

License

ISC

Last publish

Collaborators

  • diabelb