1.0.18 • Published 8 years ago
create-action-thunk v1.0.18
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
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;
}
}
1.0.18
8 years ago
1.0.17
8 years ago
1.0.16
8 years ago
1.0.15
8 years ago
1.0.14
8 years ago
1.0.12
8 years ago
1.0.11
8 years ago
1.0.10
8 years ago
1.0.9
8 years ago
1.0.8
8 years ago
1.0.7-cd
8 years ago
1.0.7
8 years ago
1.0.6
8 years ago
1.0.5
8 years ago
1.0.4
8 years ago
1.0.3
8 years ago
1.0.2
8 years ago
1.0.1
8 years ago
1.0.0
8 years ago