Last active
July 5, 2018 17:55
-
-
Save adambisek/fe6900133e4f85f0031edc6664679acf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @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