Giter VIP home page Giter VIP logo

Comments (4)

tannerlinsley avatar tannerlinsley commented on May 21, 2024 6

Since react-static builds static versions of your site, most times you'll need to make sure the window object is available (which is essentially making sure you are in a browser environment). For most cases, simply wrapping your browser-only code in something like this is sufficient:

if (typeof window !== 'undefined') {
  // browser-only code
}

from react-static.

ineghi avatar ineghi commented on May 21, 2024

Hi guys !

Is React static can be used in other context than browser ?
If no, could we assume document and window are globally available and get rid on conditionnal imports and stuff like that everywhere ? Or better, an option to make some variables globally accessible in the react static config, to let the project versatile.

let SmoothScroll
if (typeof window !== 'undefined') {
    SmoothScroll = require('smooth-scroll')
}

this exemple works, but when you are using multiple front libraries it quickly become a mess ^^

What you think ?

from react-static.

tconn89 avatar tconn89 commented on May 21, 2024

Hello, I'm also having troubles with window variable.

When I run react-static start the bundle compiles successfully, even though I have things like

componentDidMount(){
  window.addEventListener('resize', this.myFlexStyle)
}

But when I run react-static build it fails. Why does the first command above succeed?

from react-static.

tannerlinsley avatar tannerlinsley commented on May 21, 2024

You must write node safe code. Using window without a document check will error in the build stage because there is no DOM. https://react-static.js.org/docs/concepts#writing-universal-node-safe-code

from react-static.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.