Skip to content

Instantly share code, notes, and snippets.

@MoOx
Created September 8, 2016 22:16
Show Gist options
  • Save MoOx/92733134233c8bacfc43385569a50d8f to your computer and use it in GitHub Desktop.
Save MoOx/92733134233c8bacfc43385569a50d8f to your computer and use it in GitHub Desktop.
avoid "windows is undefined" error when in node for React component
import React, { Component } from "react"
import styles from "./index.css"
export default class Hero extends Component {
componentDidMount() {
if (typeof window !== "undefined") {
require("particles.js").particlesJS.load("particles-js", "assets/particles.json", function() {
console.log("callback - particles.js config loaded")
})
}
}
render() {
return (
<div id="particles-js" className={ styles.container }>
<header className={ styles.header }>
{ "Scale Conf" }
</header>
</div>
)
}
mport React, { Component } from "react"
let particles
if (typeof window !== "undefined") {
particles = require("particles.js")
}
import styles from "./index.css"
export default class Hero extends Component {
componentDidMount() {
particles && .particlesJS.load("particles-js", "assets/particles.json", function() {
console.log("callback - particles.js config loaded")
})
}
render() {
return (
<div id="particles-js" className={ styles.container }>
<header className={ styles.header }>
{ "Scale Conf" }
</header>
</div>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment