1.0.18 • Published 6 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
6 years ago
1.0.17
6 years ago
1.0.16
6 years ago
1.0.15
6 years ago
1.0.14
6 years ago
1.0.12
6 years ago
1.0.11
6 years ago
1.0.10
6 years ago
1.0.9
6 years ago
1.0.8
6 years ago
1.0.7-cd
6 years ago
1.0.7
6 years ago
1.0.6
6 years ago
1.0.5
6 years ago
1.0.4
6 years ago
1.0.3
6 years ago
1.0.2
6 years ago
1.0.1
6 years ago
1.0.0
6 years ago