Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
187
rated 0 times [  194] [ 7]  / answers: 1 / hits: 69994  / 8 Years ago, fri, july 15, 2016, 12:00:00

I have a store with a list of items. When my app first loads, I need to deserialize the items, as in create some in-memory objects based on the items. The items are stored in my redux store and handled by an itemsReducer.



I'm trying to use redux-saga to handle the deserialization, as a side effect. On first page load, I dispatch an action:



dispatch( deserializeItems() );


My saga is set up simply:



function* deserialize( action ) {
// How to getState here??
yield put({ type: 'DESERISLIZE_COMPLETE' });
}

function* mySaga() {
yield* takeEvery( 'DESERIALIZE', deserialize );
}


In my deserialize saga, where I want to handle the side effect of creating in-memory versions of my items, I need to read the existing data from the store. I'm not sure how to do that here, or if that's a pattern I should even be attempting with redux-saga.


More From » reactjs

 Answers
11

you can use select effect



import {select, ...} from 'redux-saga/effects'

function* deserialize( action ) {
const state = yield select();
....
yield put({ type: 'DESERIALIZE_COMPLETE' });
}


also you can use it with selectors



const getItems = state => state.items;

function* deserialize( action ) {
const items = yield select(getItems);
....
yield put({ type: 'DESERIALIZE_COMPLETE' });
}

[#61347] Thursday, July 14, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tomas

Total Points: 165
Total Questions: 111
Total Answers: 103

Location: Maldives
Member since Tue, Dec 21, 2021
2 Years ago
tomas questions
Thu, Jan 27, 22, 00:00, 2 Years ago
Mon, May 10, 21, 00:00, 3 Years ago
Tue, Jan 5, 21, 00:00, 3 Years ago
;