0

In almost all my sagas I call a selector which gets the organizationId from the state, like so:

const organizationId = yield select(getOrganizationId);

My rootSaga is set up like so:

export function* rootSaga() {
    yield all([
        fork(someSaga),
        fork(someOtherSaga),
    ]);
}

In this example, both someSaga and someOtherSaga would call getOrganizationId.

What I would want instead is to make the select in rootSaga, and pass down the value to all forked sagas. The value I pass down would have to be updated as the selector's result updates.

How can I achieve this?

Willy G
  • 1,172
  • 1
  • 12
  • 26

1 Answers1

3

You can pass it as a second argument for the fork effect. Example:

    export function* rootSaga() {
        const organizationId = yield select(getOrganizationId);
        yield all([
            fork(someSaga, organizationId),
            fork(someOtherSaga, organizationId),
        ]);
    }
    
    function* someSaga(organizationId) {
      // You can use organizationId here
    }

Refer API docs for more details.

Icaro
  • 14,585
  • 6
  • 60
  • 75
vahissan
  • 2,322
  • 16
  • 26
  • Tried this approach and the selector returns undefined since the select is made while the store is being initialized. The yield select doesn't seem to update when the selector's return value updates (as with vanilla selectors). Do you know of any straightforward approach to get around this? – Willy G Aug 20 '18 at 15:43