Understanding redux-saga: From action creators to sagas
blog.logrocket.com
blog.logrocket.com
const getDog = dogId => (dispatch, getState, { DogApi }) => {
dispatch(fetchingDog(dogId))
DogApi.getDogById(dogId, (err, dog) => {
dispatch(err ? dogFetchError(err) : fetchedDog(dog))
})
}
...
dispatch(getDog(123)) function myAction () {
return {
type: 'MY_ACTION',
payload: someApiCall(),
}
}
And that will dispatch `MY_ACTION_PENDING`, then `MY_ACTION_FULFILLED` or `MY_ACTION_REJECTED`. Soon you'll be able to write your payload as an `async function`: function myAction () {
return {
type: 'MY_ACTION',
async payload (dispatch) {
const apiResult = await someApiCall()
dispatch(anotherActionCauseWhyNot(apiResult))
return apiResult
}
}
}
I'm sure there's more to redux-saga, but this article doesn't do a great job of presenting that.export default function* playGame({ getState, selectors }) { yield take(GAME_READY);
const refreshDelay = 1000 / FPS;
while (true) {
yield take(START_GAME_PLAY);
while (true) {
const {
gameState: {
speed,
isPlaying,
gameTime,
},
} = getState();
if (!isPlaying) {
// game play has stopped
break;
}
const timeDelta = refreshDelay * speed;
yield put(setGameTime(gameTime + timeDelta));
// sleep
yield call(delay, refreshDelay);
}
}
}