Skip to content
This repository has been archived by the owner on Aug 29, 2021. It is now read-only.

Commit

Permalink
Check window.location in componentDidMount
Browse files Browse the repository at this point in the history
Fixes a Gatsby build error where window is not available.

Refer to: gatsbyjs/gatsby#309
  • Loading branch information
nbw committed Dec 3, 2018
1 parent d567904 commit 15ece8d
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/pages/index.js
Expand Up @@ -19,19 +19,32 @@ const TIME24HR = "HH:mm";
const TIME12HR = "h:mm a";

// Pull default state from the URL if available.
const urlCoder = URLCoder.fromURL(window.location);

class IndexPage extends Component {
state = {
timeFormat: TIME12HR,
locA: urlCoder.locA || {},
locAStart: urlCoder.locAStart || DEFAULT_START_TIME,
locAEnd: urlCoder.locAEnd || DEFAULT_END_TIME,
locB: urlCoder.locB || {},
locBStart: urlCoder.locBStart || DEFAULT_START_TIME,
locBEnd: urlCoder.locBEnd || DEFAULT_END_TIME,
locA: {},
locAStart: DEFAULT_START_TIME,
locAEnd: DEFAULT_END_TIME,
locB: {},
locBStart: DEFAULT_START_TIME,
locBEnd: DEFAULT_END_TIME,
}

componentDidMount() {
const urlCoder = URLCoder.fromURL(window.location);
if (urlCoder) {
this.setState({
locA: urlCoder.locA,
locAStart: urlCoder.locAStart,
locAEnd: urlCoder.locAEnd,
locB: urlCoder.locB,
locBStart: urlCoder.locBStart,
locBEnd: urlCoder.locBEnd,
})
}
}

update = (name, value) => {
this.setState({
[name]: value
Expand Down

0 comments on commit 15ece8d

Please sign in to comment.