Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kellyrmilligan/f0b32d59a6fb3690fc6aa301bf77504f to your computer and use it in GitHub Desktop.
Save kellyrmilligan/f0b32d59a6fb3690fc6aa301bf77504f to your computer and use it in GitHub Desktop.
import React, { Component } from 'react'
import { withRouter } from 'react-router'
import reactRouterFetch from 'react-router-fetch'
class App extends Component {
state = {
isAppFetching: false,
appFetchingError: null
}
componentWillMount () {
this.fetchRoutes(this.props)
}
componentWillReceiveProps (nextProps) {
const current = `${this.props.location.pathname}${this.props.location.search}`
const next = `${nextProps.location.pathname}${nextProps.location.search}`
if (current === next) {
return
}
this.fetchRoutes(nextProps)
}
shouldComponentUpdate (nextProps, nextState) {
return !nextState.isAppFetching
}
fetchRoutes (props) {
const { dispatch, location } = props
this.setState({
isAppFetching: true,
appFetchingError: null
})
//maybe show a progress bar somewhere outside of react? go nuts!!
reactRouterFetch(routeConfig, location, { dispatch })
.then((results) => {
this.setState({
isAppFetching: false
})
})
.catch((err) => {
this.setState({
isAppFetching: false,
appFetchingError: err
})
})
}
render () {
//do something with isAppFetching for the first render if single page app.
// after the first render, the page contents will stay put until the next route's data is ready to go, so you'll have to do something outside of this.
return (
...
)
}
}
const connectedApp = connect()(App)
export default withRouter(connectedApp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment