React-redux-starter-kit: isFetching best practice

Created on 12 Jun 2016  路  6Comments  路  Source: davezuko/react-redux-starter-kit

Guys help me,
i have layout

<div className='container text-center'>
    <Header/>
     <ProgressBar mode='indeterminate' multicolor status={isFetching ? 'loading' : 'hide'} />
    <div className={classes.mainContainer}>
      {children}
    </div>
  </div>

{children} - redux component which use ajax request for get data.
How can i activate ProgressBar from children?

Most helpful comment

@kodermax Not the prettiest solution but it works if yours async action are in format FOO_REQUEST, FOO_SUCCESS, FOO_FAILURE

// progressMiddleware.js //
import { showProgressBar, hideProgressBar } from '../modules/progress'

export default ({ dispatch, getState }) => next => action => {
  // FOO_REQUEST => REQUEST //
  const requestType = action.type.slice(action.type.lastIndexOf('_') + 1)
  const isProgressBarShown = getState().progress
  if (requestType === 'REQUEST' && !isProgresBarShown) {
    dispatch(showProgressBar()) // Sets state.progress to true
  } else if (requestType === 'SUCCESS' || requestType === 'FAILURE' && isProgresBarShown) {
    dispatch(hideProgressBar()) // Sets state.progress to false
  }
  return next(action)
}

All 6 comments

you might want to create some sort of global isFetching that checks to see if any of the child isFetchings are set to true than turn the progress bar on. One way you could implement that is via a compound selector (using reselect) that checks all the isFetching input selectors for truthiness.

Alternatively you could just do one big || statement like:

mapStateToProps = state => ({
  globalFetching: state.posts.isFetching || state.todos.isFetching ... etc.
});

Hope that helps/gives you direction!

Thank you @SpencerCDixon

You could have a store/reducer that will catch an action dispached from the middleware( used for all async actions) everytime an ajax call is triggered and finised. U could have a counter in the store for that. U could connect and get the counter in the class that connects to the store and if the total count is bihlgger than 0 show the progress bar.

@razvanip are there any example of this?

@kodermax Not the prettiest solution but it works if yours async action are in format FOO_REQUEST, FOO_SUCCESS, FOO_FAILURE

// progressMiddleware.js //
import { showProgressBar, hideProgressBar } from '../modules/progress'

export default ({ dispatch, getState }) => next => action => {
  // FOO_REQUEST => REQUEST //
  const requestType = action.type.slice(action.type.lastIndexOf('_') + 1)
  const isProgressBarShown = getState().progress
  if (requestType === 'REQUEST' && !isProgresBarShown) {
    dispatch(showProgressBar()) // Sets state.progress to true
  } else if (requestType === 'SUCCESS' || requestType === 'FAILURE' && isProgresBarShown) {
    dispatch(hideProgressBar()) // Sets state.progress to false
  }
  return next(action)
}

@kolpav thank you.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

maxkrieger picture maxkrieger  路  4Comments

jokeyrhyme picture jokeyrhyme  路  5Comments

orielz picture orielz  路  3Comments

nickgnd picture nickgnd  路  4Comments

zeroc picture zeroc  路  4Comments