I'm using redux
with redux-thunk
for async actions.
And I found a lot of similar questions on this site, also on some other blogs -
some people suggest not to use getState()
in action-creator because it's anti-pattern, from other hand other people says that it's OK in some specific cases. But I still can't understand is it OK to do it in my specific case.
I have auth
reducer which contains userId
inside it.
And this is my action creator:
export const getUserHobbies= () => (dispatch, getState )=> {
const currentUserId = getState().auth.userId;
dispatch({
type: GET_HOBBIES_REQUEST
});
return fetch(`api/hobbies/?userId=${currentUserId}`)
.then (json => {
//...
})
.catch( e => {
//...
})
};
As you can see I need current user ID for doing AJAX request, and I'm retrieving current user ID using getState()
in action creator. Is it OK? Or better connect userId
to my component and pass this value to action-creator from component?