Max Wolfen
Max Wolfen

Reputation: 2023

Redux_Saga. How to past the function as an argument to the nested Generator?

So, I want to push some function setupSocket as the argument to execution inside the nested Generator createSaga, but now I cannot understand how can I make it?

Inside the parent Generator mySaga the setupSocket displayed normally, but inside the createSaga nope...

import { takeEvery, put, call, take } from 'redux-saga/effects'; 
import { delay } from 'redux-saga';
import { addMessage, addUser } from '../actions'
import * as constants from '../constants'

function* createSaga(action, setupSocket) {
    try {
        console.log(setupSocket); // not good
        yield put(addMessage(action.message, action.author, action.photo));
        yield put(addUser(action.author, action.photo));
    } catch (e) { 
        console.log(e) 
    }
};

function* mySaga(setupSocket) {
    console.log(setupSocket); // all good
    yield takeEvery(constants.ADD_DATA, createSaga);
};

export default mySaga;

Upvotes: 3

Views: 668

Answers (1)

Martin Kadlec
Martin Kadlec

Reputation: 4975

You can pass additional arguments to takeEvery that will be passed down to the saga.

function* createSaga(setupSocket, action) {
    console.log(setupSocket); // should now be good
    ...
};

function* mySaga(setupSocket) {
    yield takeEvery(constants.ADD_DATA, createSaga, setupSocket);
};

If you pass down some arguments, action will be always the last one so I had to swap action and setupSaga order in the createSaga saga arguments.

https://redux-saga.js.org/docs/api/#takeeverypattern-saga-args

Upvotes: 2

Related Questions