Redux-saga setContext недоступен в других сагах - PullRequest
0 голосов
/ 26 октября 2018

Не уверен, что я делаю не так, я просто хотел бы, чтобы контекст был доступен из всех саг.

// Saga.js
function* test1() {
  const foo = yield getContext('foo');
  if (!foo) yield setContext({ foo: 'bar' });
  const test = yield getContext('foo');
  console.log(test); // Correct 'bar'.
}

function* test2() {
  const getFooValue = yield fork(test1); // This doesnt return getContext or the context value of foo
  // Do stuff here.
}

И промежуточное ПО

// TheStore.js
const sagaMiddleware = createSagaMiddleware({
  context: {
    foo: '',
  },
});

const TheStore: Store<ReduxState, *> = createStore(
  reducers,
  applyMiddleware(sagaMiddleware)
);

1 Ответ

0 голосов
/ 02 ноября 2018

Если вы хотите вернуть значение foo, вы можете сделать следующее:

// Saga.js
function* test1() {
  const foo = yield getContext('foo');
  if (!foo) yield setContext({ foo: 'bar' });
  const test = yield getContext('foo');
  console.log(test); // Correct 'bar'.
  return test;
}

function* test2() {
  const getFooValue = yield call(test1); // This will now return the value of foo
  // Do stuff here.
}

fork не будет ждать завершения test1 перед выполнением следующих строк кода.call будет ждать окончания test1 и получит возвращаемое значение.

...