Skip to content

Instantly share code, notes, and snippets.

@a-barbieri
Last active March 21, 2019 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a-barbieri/2d8db731d0862fe3e72aad13ad00bc0e to your computer and use it in GitHub Desktop.
Save a-barbieri/2d8db731d0862fe3e72aad13ad00bc0e to your computer and use it in GitHub Desktop.
Saga sequence

Saga Sequence with NextJS

Requirements:

  1. You need to have set up next-redux-saga package.
  2. Some understanding of how Redux Saga work is needed.

The example isn't complete with all files, but if know how Redux Saga works you might imagine yourself how the missing file look like.

If something isn't clear I can provide more info and update the gist.

import { GET_FIRST_AND_SECOND } from "actions/types";
export function getFirstAndSecondAction() {
return {
type: GET_FIRST_AND_SECOND
};
}
import React, { Component } from "react";
import { getFirstAndSecondAction } from "actions/getFirstAndSecondAction";
class MyComponent extends Component {
static async getInitialProps({ store }) {
// Trigger the saga sequence
store.dispatch(getFirstAndSecondAction());
return {};
}
render() {
return (
<div>
<p>My Component</p>
</div>
);
}
}
export default MyComponent;
import {
call,
select,
takeLatest
} from "redux-saga/effects";
import { fistSaga } from "sagas/fistSaga";
import { secondSaga } from "sagas/secondSaga";
import * as actionTypes from "actions/types";
const getSomeData = state => state.someData;
export function* getFirstAndSecondWatcher() {
const saga = yield takeLatest(actionTypes.GET_FIRST_AND_SECOND, firstAndSecondSaga);
}
export function* firstAndSecondSaga() {
// Call the first saga
yield call(fistSaga, { type: actionTypes.GET_FIRST });
// Once the first saga is completed get the result from the store (this time we get `someData` reducer)
const someData = yield select(getSomeData);
// Call the second saga with the info provided by the first one
yield call(secondSaga, { type: actionTypes.GET_SECOND, data: someData });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment