redux-thunk-generators
v0.3.1
Published
Thunk middleware for Redux supporting generators
Downloads
9
Maintainers
Readme
Redux thunk + generators
You can use generators as action creators or thunks. Fully compartible with redux-thunk!
Installation
npm install --save redux-thunk-generators
Just replace redux-thunk import with redux-thunk-generators
Usage
You can use generators (sync or async) as thunks:
export const signIn = payload => async function* (dispatch, getState, extraArgument) { /* ... */ }
Or use generators as action creators:
export const signIn = async function* (payload) { /* ... */ }
Yield action objects to dispatch them! Forget about wrapping each time with dispatch
:
// Action creator
export const signIn = async function* (payload) {
const { username, password } = payload;
let state = yield; // won't be dispatched, just returns current state
yield signInStart();
try {
const response = await axios.post(API_SIGN_IN, { username, password });
yield signInEnd();
yield signInSuccess(response.data);
return username;
} catch (error) {
yield signInEnd();
yield signInError(error);
}
};
yield
always returns a (new) state.
If you want to do something when your action is done, return
some data from generator and get it with .then:
signIn().then(username => {
console.log(username)
});
Yep, nice) Tell your friend.
Author
@doasync