简体   繁体   English

redux-saga注射两次

[英]redux-saga injected twice

I have a redux-saga which is called once, but it is executing twice. 我有一个被称为一次的redux-saga,但它执行了两次。

This is the action that starts the saga: 这是开始传奇的动作:

export function createRequest (data) {
  return {
    type: CREATE_REQUEST,
    payload: {data}
  };
}

and my sagas.js file looks this way: 我的sagas.js文件看起来是这样的:

export function* create (x) {
  try {
    const response = yield call(request, URL_TO_API, Object.assign({}, buildBaseHeaders('en'), {
      method: 'post',
      body: JSON.stringify(x.payload.data)
    }));
    yield put(createSuccess(response));
  } catch (error) {
    yield put(createFailure(error));
  }
}

... my other sagas

export default function* defaultSaga () {
  yield takeLatest(CREATE_REQUEST, create);
  ... my other calls
}

The way I'm injecting the sagas into my React component is this: 我将Sagas注入我的React组件的方式是这样的:

const withConnect = connect(mapStateToProps, mapDispatchToProps);
const withReducer = injectReducer({key: 'myComponent', reducer});
const withSaga = injectSaga({key: 'myComponent', saga});

export default compose(withReducer, withSaga, withConnect) MyComponent;

But the saga is injected twice. 但是传奇被注射了两次。 So, what am I missing here? 那么,我在这里想念什么? How can I inject the saga only once no matter on how many times MyComponent gets rendered? 无论MyComponent渲染了多少次,我如何都只能注入传奇一次?

But the saga is injected twice. 但是传奇被注射了两次。 So, what am I missing here? 那么,我在这里想念什么?

Solution is dependent on how redux-sagas-injector npm library works. 解决方案取决于redux-sagas-injector npm库的工作方式。 In general case, asynchronous loading and applying for sagas is difficult thing, because saga is consists of "live" process manager, which can not be disposed on some function call or object deletion. 在一般情况下,异步加载和应用Sagas是一件困难的事情,因为saga由“实时”进程管理器组成,不能在某些函数调用或对象删除上进行处理。
It implies from saga's ability to launch custom tick callback domains (Promises, AJAX/XHR, setImmediate , etc), which can not be disposed from custom external code (That's also reason, why HMR does not work with sagas in partial mode and should reload whole page). 它暗示着saga具有启动自定义滴答回调域(Promises,AJAX / XHR, setImmediate等)的能力,而该域无法从自定义外部代码中setImmediate (这也是原因,为什么HMR无法在部分模式下使用sagas并应重新加载)整个页面)。

So, if you perform saga injection on router switching, check two things: that old saga has implicit action to dispose from outer side, like special inner technical dispose action, and that there is not misconfiguration in router side - maybe same page had been launched, for example, twice. 因此,如果您在路由器交换上执行saga注入,请检查两件事:旧的Saga具有从外部进行隐式处理的操作(例如特殊的内部技术处理操作),并且在路由器端没有配置错误-可能启动了同一页面,例如两次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM