Comments (11)
@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.
@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.
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.
because server can not find window, you can use this
getInitialState() {
return {windowWidth: (typeof window !== 'undefined')? window.innerWidth:undefined};
},
from react-starter.
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.
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.
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.
@Frikki thank you! had the same issue and this fix it. 👍
from react-starter.
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.
@troutowicz thank you!
from react-starter.
Hy all
I have this problem for server rendering.
from react-starter.
Related Issues (20)
- Can't find container issue HOT 1
- What is best way to handle user authentication?
- Cannot find module 'tapable' HOT 8
- Does not work with current version of react-router 1.0.0 HOT 1
- Can's see any css style in page
- Failed Proptype warning
- [OUTDATED]? HOT 3
- npm start
- wwe
- Help me :( Can't start webpack-dev-server
- Get requested domain?
- Error defining includePaths for sass-loader HOT 1
- Request: a way to use the production React build with this HOT 2
- No easy way to include "global" CSS HOT 1
- How to exclude files from prerender? HOT 2
- Where to put <head> HOT 2
- dev server should proxy api instead of vice versa
- Plugin system?
- React Router injected `query` prop
- vagrant installation method issues HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-starter.