D
D
dufrein20132020-03-05 23:26:56
In contact with
dufrein2013, 2020-03-05 23:26:56

How to put an action in a callback using redux saga and if you need an event channel?

Good afternoon, redux saga gurus) I just started to master it ...
There was a task to make authentication on the site using VK OpenApi. At the same time, redux saga is used for logic. For this, the *someSaga saga was made in which vk.api.call is called, in the callback of which it would be nice to call yield put(someAction()), but this is impossible, because callback is not a generator.
I read that you can use the redux saga event channel: I made a separate function someFunction, where I return an eventChannel (as described in the redux saga docs and in it I do emit (data) of the data that vk.api returned to me), then I call it in the previously created saga with using
const callVkChannel = yield call(()=>someFunction())
const data = yield take(callVkChannel ) (and the authorization data is successfully written to data)
but at the same time, all the other actions that are sent using put in other sagas somehow get there sideways.
The question is why is that?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Bone, 2020-03-06
@dufrein2013

There was a similar problem. I decided that the easiest way would be to pull store.dispatch directly, and not through put.
1. Pass store.dispatch to the root saga:

const sagaMiddleware = createSagaMiddleware();
const store = configureStore({
    reducer: {
        app: VkAppReducer,
    },
    middleware: [
        sagaMiddleware,
    ],
});
sagaMiddleware.run(rootSaga, store.dispatch);

2. From the root saga, pass dispatch to the children
function* rootSaga(dispatch) {
    yield spawn(ChildSaga, dispatch);
}

3. In the child saga, we can use dispatch in the callback:
const params: MethodParams = {
            group_id: 1,
            access_token: access_token,
            onProgress: (s: string) => {
                dispatch(setProgress(s));
            },
        };

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question