getState in redux-saga?

前端 未结 2 1697
余生分开走
余生分开走 2021-01-30 07:55

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

相关标签:
2条回答
  • 2021-01-30 08:34

    Select effect does not help us if we in a callback functions, when code flow is not handled by Saga. In this case just pass dispatch and getState to root saga:

    store.runSaga(rootSaga, store.dispatch, store.getState)
    

    And the pass parameters to child sagas

    export default function* root(dispatch, getState) { yield all([ fork(loginFlow, dispatch, getState), ]) }

    And then in watch methods

    export default function* watchSomething(dispatch, getState) ...

    0 讨论(0)
  • 2021-01-30 08:45

    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' });
    }
    
    0 讨论(0)
提交回复
热议问题