Skip to content

Instantly share code, notes, and snippets.

@lexfrl
Last active August 29, 2015 14:22
Show Gist options
  • Save lexfrl/0f7b44afc9fd29f36daf to your computer and use it in GitHub Desktop.
Save lexfrl/0f7b44afc9fd29f36daf to your computer and use it in GitHub Desktop.
import React, { PropTypes } from "react";
import Logo from "../components/Logo";
import slots from "../slots";
const Location = React.createClass({
getInitialState() {
return {
pushState: true
};
},
componentDidMount() {
if (!process.env.BROWSER) {
return;
}
window.onpopstate = (e) => {
this.setState({pushState: false});
slots.set("request.url", window.location.href);
}
},
componentWillReceiveProps(nextProps) {
if (!process.env.BROWSER) {
return;
}
if (!this.shouldComponentUpdate(nextProps)) {
return;
}
if (this.state.pushState) {
window.history.pushState(null, null, nextProps.url);
}
this.setState({pushState: true});
},
shouldComponentUpdate(nextProps) {
return nextProps.url != this.props.url;
},
render() {
return null;
}
});
export default Location;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment