反应 redux-saga 在哪里处理一些逻辑
react redux-saga where to handle some logic
我正在使用 redux-saga 和 React。可以说我正在从需要一些外部客户端计算的服务器中获取一些数据。我的问题是把它们放在哪里。
我的故事看起来像这样:
function* someEffect(action){
try{
//removed none related stuff from the code.
const data = yield call(someRequestFucntionThatUsesAxios, requestparams);
if(data.status>=200&&data.status<300){
yield put({type:SUCCESS, payload:data.data)};
}
catch(err){
}
}
在上面的代码中 data.data
需要一些计算(主要是基础数学)。现在,这些选项中的哪一个更有意义?
在我的连接组件 componentDidUpdate
方法中处理这些计算。
收到后立即处理它们(在发送到 reducer 之前)。
(我知道 reducer 不适合这种行为,但因为它不会杀死询问,)在我的 reducer 中处理它。
正如您所说,reducer
不是修改 API 响应的最佳位置,saga 也不是。
我建议你在 axios 中使用 transformResponse
函数,在从 API.
返回后立即修改响应
axios.get('/', {
transformResponse: (data) => {
// do whatever you like with the data
},
});
我正在使用 redux-saga 和 React。可以说我正在从需要一些外部客户端计算的服务器中获取一些数据。我的问题是把它们放在哪里。 我的故事看起来像这样:
function* someEffect(action){
try{
//removed none related stuff from the code.
const data = yield call(someRequestFucntionThatUsesAxios, requestparams);
if(data.status>=200&&data.status<300){
yield put({type:SUCCESS, payload:data.data)};
}
catch(err){
}
}
在上面的代码中 data.data
需要一些计算(主要是基础数学)。现在,这些选项中的哪一个更有意义?
在我的连接组件 componentDidUpdate
方法中处理这些计算。
收到后立即处理它们(在发送到 reducer 之前)。
(我知道 reducer 不适合这种行为,但因为它不会杀死询问,)在我的 reducer 中处理它。
正如您所说,reducer
不是修改 API 响应的最佳位置,saga 也不是。
我建议你在 axios 中使用 transformResponse
函数,在从 API.
axios.get('/', {
transformResponse: (data) => {
// do whatever you like with the data
},
});