Pavel
Pavel

Reputation: 2193

Redux: getState in action

I change the general state through other actions and when I need to fetch posts I'm doing this:

export const fetchPosts = () => (dispatch, getState) => {

  let state = getState().postReducer
  let options = {
    order: state.order,
    page: state.page,
    per: state.per,
    search: state.search
  }
  let params = generateParams(options)

  return fetch(`${BASE_URL}/api/posts?${params}`)
    .then(response => response.json())
    .then(response => dispatch(receivePosts(response)))
}

Is it ok or not?

Upvotes: 2

Views: 1456

Answers (1)

FurkanO
FurkanO

Reputation: 7308

Using getState to get the state and use as you did is ok. Nothing is wrong about it. Even I find it easier to maintain rather than getting the redux state and passing to action creators from components.

Upvotes: 2

Related Questions