Skip to content

Instantly share code, notes, and snippets.

@adambisek
Last active July 5, 2018 17:55
Show Gist options
  • Save adambisek/fe6900133e4f85f0031edc6664679acf to your computer and use it in GitHub Desktop.
Save adambisek/fe6900133e4f85f0031edc6664679acf to your computer and use it in GitHub Desktop.
// @flow
import React from 'react'
export default class Router extends React.PureComponent {
constructor(props) {
super(props)
this.state = {}
}
componentDidMount() {
this.fetch(this.props.location.pathname).then(data => this.setState({ data }))
}
componentWillReceiveProps(nextProps) {
if (this.isLocationEqual(this.props.location, nextProps.location)) return
console.log('Page: location changed to:', this.props, nextProps)
this.setState({ data: null }) // loading
this.fetch(nextProps.location.pathname).then(data => this.setState({ data }))
}
render() {
if (!this.state.data) return this.renderLoadingComponent(this.props)
return this.renderComponent({ ...this.props, ...this.state.data }) // this function also takes care of sending headers, HTTP code, etc.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment