punjira
punjira

Reputation: 836

react redux-saga where to handle some logic

I am using redux-saga with React. Lets say i'm fetching some data from server that need some external client side calculation. My question is where to put them. my saga looks something like this:

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){
    }
}

In the above code data.data needs some calculation(basic math mostly). Now, which one of these options makes more sense?
handling those calculations in my connected components componentDidUpdate method.
handling them right after receiving it (before dispatching to to reducer).
(I know that reducer is no place for such behaviour but since it doesn't kill to ask,) handling it inside my reducer.

Upvotes: 1

Views: 200

Answers (1)

kind user
kind user

Reputation: 41893

As you have said reducer is not the best place to modify the API response, neither saga is.

I would suggest you to use transformResponse function in axios, to modify the response just right after it is returned from API.

axios.get('/', {
   transformResponse: (data) => { 
      // do whatever you like with the data
   },
});

Upvotes: 1

Related Questions