GithubHelp home page GithubHelp logo

Comments (11)

troutowicz avatar troutowicz commented on April 28, 2024 43

@Frikki Dependency declarations should be kept at the top of files. A better solution for your case would be to do a conditional require at the top of your component.

const isBrowser = typeof window !== 'undefined';
const MyWindowDependentLibrary = isBrowser ? require( 'path/to/library') : undefined;

from react-starter.

Frikki avatar Frikki commented on April 28, 2024 32

@sokra How do you exclude a library from prerendering?

Update
I figured I shouldn’t import the library outside the class definition, but instead require it in the componentDidMount method or a called method thereof.

So, instead of:

...
import MyWindowDependentLibrary from 'path/to/library';
...
export default class MyClass extends React.Component {
    ...
    componentDidMount() { MyWindowDependentLibrary.doWork(); }
    ...
}

I did:

// removed import
...
export default class MyClass extends React.Component {
    ...
    componentDidMount() {
        const MyWindowDependentLibrary = require( 'path/to/library' );
        MyWindowDependentLibrary.doWork();
    }
    ...
}

from react-starter.

barrystaes avatar barrystaes commented on April 28, 2024 17

It looks like you are using window somewhere (for jQuery ?) and this variable sometimes (notably serverside) sometimes does not exist.

Try this fix i did in my ReactPlayground: https://github.com/barrystaes/ReactPlayground/commit/111503b23daaecc944ca0ce8f6ffa6430cd4b509

from react-starter.

17Damon avatar 17Damon commented on April 28, 2024 3

because server can not find window, you can use this

getInitialState() {
return {windowWidth: (typeof window !== 'undefined')? window.innerWidth:undefined};
},

from react-starter.

Andriy-Kulak avatar Andriy-Kulak commented on April 28, 2024 1

Also, another trick you can use below! :-) I ran into the same error and saw some universal/isomorphic react/node boilerplates use this

   if (process.env.BROWSER) {
      devToolsExtension = window.devToolsExtension();
    }

from react-starter.

FoxxMD avatar FoxxMD commented on April 28, 2024

thanks! I will give that a try. Also I figured out that this only happens when using the prerender build option so it's not critical as I'm not using that for production.

from react-starter.

sokra avatar sokra commented on April 28, 2024

Don't use libraries that depend on window in your components.
or
Only use these libraries in componentDidMount and exclude them for prerendering (make sure your component don't render different in prerendering or it doesn't work).

from react-starter.

juancabrera avatar juancabrera commented on April 28, 2024

@Frikki thank you! had the same issue and this fix it. 👍

from react-starter.

derekdreery avatar derekdreery commented on April 28, 2024

You can't use conditional requires in ES6 module syntax. For this I tend to write no-op implementations of anything that is client-specific (alternatively they could throw on the server, to point out that their use is an error).

from react-starter.

rihannarickeminem avatar rihannarickeminem commented on April 28, 2024

@troutowicz thank you!

from react-starter.

 avatar commented on April 28, 2024

Hy all
I have this problem for server rendering.

from react-starter.

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.