【发布时间】:2020-06-01 12:06:25
【问题描述】:
我有一个 react 应用程序,我正在其中创建一个单元,它需要授权。
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);
}
我应该从组件中发送令牌作为有效负载的一部分,还是应该从存储在 saga 中的用户状态中选择令牌?因为在我看来,选择令牌 mapStateToProps 然后在我可以从 saga 中选择令牌时将其与操作一起发送是很复杂的
【问题讨论】:
标签: reactjs redux axios token redux-saga