1.0.18 • Published 7 years ago

create-action-thunk v1.0.18

Weekly downloads
2
License
ISC
Repository
github
Last release
7 years ago

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

1.0.18

7 years ago

1.0.17

7 years ago

1.0.16

7 years ago

1.0.15

7 years ago

1.0.14

7 years ago

1.0.12

7 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7-cd

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago