ref – https://stackoverflow.com/questions/38405700/getstate-in-redux-saga
We want to listen for DESERIALIZE actions. Once we detect one, we want to run function deserialize.
1 2 3 |
function* mySaga() { yield* takeEvery( 'DESERIALIZE', deserialize ); } |
where deserialize is:
1 2 3 4 |
function* deserialize( action ) { // How to getState here?? yield put({ type: 'DESERISLIZE_COMPLETE' }); } |
Problem is, how do we get state of the store?
The answer is that we can use select to the store’s state.
1 2 3 4 5 6 7 |
import {select, ...} from 'redux-saga/effects' function* deserialize( action ) { const state = yield select(); .... yield put({ type: 'DESERIALIZE_COMPLETE' }); } |
You can also declare a function that filters out what state properties you need, and select will literally “select” the data from the store for you
1 2 3 4 5 6 7 |
const getItems = state => state.items; function* deserialize( action ) { const items = yield select(getItems); .... yield put({ type: 'DESERIALIZE_COMPLETE' }); } |