mangocaptain mangocaptain - 2 months ago 14
React JSX Question

What is the purpose of action functions in react redux? Should they all return an action or function that goes to the store/middleware?

In the react redux reddit app example should the #shouldFetchPosts be part of the actions? It seems weird that it's more of a helper than an actual action, which should return an object that is dispatched to the store. I understand that actions can include functions that serve to send async related things to the middleware, but #shouldFetchPosts doesn't seem to fit this purpose nor one of a traditional action. Is there a better way of organizing this function? Should it of not been abstracted out at all in the first place?

function fetchPosts(reddit) {
return dispatch => {
return fetch(`${reddit}.json`)
.then(response => response.json())
.then(json => dispatch(receivePosts(reddit, json)))

function shouldFetchPosts(state, reddit) {
const posts = state.postsByReddit[reddit]
if (!posts) {
return true
if (posts.isFetching) {
return false
return posts.didInvalidate

export function fetchPostsIfNeeded(reddit) {
return (dispatch, getState) => {
if (shouldFetchPosts(getState(), reddit)) {
return dispatch(fetchPosts(reddit))


Good catch. And yes, you're right! shouldFetchPosts is not an action. It's a helper function. It should not be placed with action creators in the first place. One may think that the redux examples have a huge scope for improvement. But on the brighter side, it's a great way to learn things too. You spotted a mistake in their code. You should go ahead and correct it in your sample project.