如何从Redux-Saga中选择状态

问题描述 投票:1回答:1

我有一个React App,我正在其中创建单位,并且需要授权。

function* createUnitWorker(action) {
  const { payload: {unitDetails, history} } = action;
  try {
    const unit = yield axios({
      method: 'post',     
      url: `https://myBackend/units/`,
      headers: {'Authorization': 'Bearer'+token}, 
      data: {
         ...unitDetails
      }
    });
    yield put(call(createUnitSuccess, unit));
    yield history.push(`/unit/${unit.code}`)
  } catch (error) {
    yield put(createUnitFailure(error));
    yield put(history.push('/error'))
  }
}
export function* createUnitWatcher() {
  yield takeLatest(unitActionTypes.CREATE_UNIT_START, createUnitWorker);
}

我应该从组件中发送令牌作为有效负载的一部分,还是应该从我存储在传奇中的用户状态中选择令牌?因为在我看来,选择标记mapStateToProps并随后在我可以从传奇中选择标记的情况下将其与操作一起发送比较复杂。

我有一个React App,我正在其中创建单位,并且需要授权。函数* createUnitWorker(操作){const {有效负载:{unitDetails,history}} =操作;尝试{const unit = yield ...

reactjs redux axios token redux-saga
1个回答
1
投票

我建议使用axios interceptors,因此您不必将令牌手动添加到您发送的每个请求中。

© www.soinside.com 2019 - 2024. All rights reserved.