Getting Started
Get up and running with saga slices quickly
What it is
Getting Started
// Create a react app if you don't already have one
npx create-react-app myApp
cd myApp
// Install saga slice and dependencies
npm i --save redux redux-saga immer saga-sliceimport { put, select, takeLatest } from "redux-saga/effects";
import { createModule } from 'saga-slice';
const sagaSlice = createModule({
// Key name that gets added to combineReducers
name: 'todos',
initialState: {
isFetching: false,
data: null,
error: null,
shouldRunOnce: 0
},
// Reducers object is the bread and butter of saga slice.
// Defining a reducer also defines a type and action.
// The type will be `todos/fetch`, using the pattern of `{name}/{key}`
reducers: {
fetch: (state) => {
state.isFetching = true;
},
fetchSuccess: (state, payload) => {
state.isFetching = false;
state.data = payload;
},
fetchFail: (state, payload) => {
state.isFetching = false;
state.error = payload;
}
},
// The sagas option is a function that gets passed the Actions object.
// Actions are converted into strings which are the value of its
// corresponding type. You can also use the actions object to dispatch
// actions from sagas using the `put` effect.
sagas: (A) => ({
*[A.fetch]({ payload }) {
try {
const { data } = yield.axios.get('/todos');
yield put(A.fetchSuccess(data));
}
catch (e) {
yield put(A.fetchFail(data));
}
}
})
});
// Export actions for convenience when importing from other modules
export const { actions } = sagaSlice;
export default sagaSlice;Last updated
Was this helpful?