this is not defined
I was using react, redux, relay, trying to achieve server render, and I found this following problem
ReferenceError: this is not defined
at Blog (index.js:183:23)
at [object Object].ReactCompositeComponentMixin.mountComponent (/Users/aihornmac/Documents/site/ihawn/code/node_modules/react/lib/ReactCompositeComponent.js:148:18)
at [object Object].wrapper [as mountComponent] (/Users/aihornmac/Documents/site/ihawn/code/node_modules/react/lib/ReactPerf.js:66:21)
at Object.ReactReconciler.mountComponent (/Users/aihornmac/Documents/site/ihawn/code/node_modules/react/lib/ReactReconciler.js:37:35)
at [object Object].ReactCompositeComponentMixin.mountComponent (/Users/aihornmac/Documents/site/ihawn/code/node_modules/react/lib/ReactCompositeComponent.js:225:34)
at [object Object].wrapper [as mountComponent] (/Users/aihornmac/Documents/site/ihawn/code/node_modules/react/lib/ReactPerf.js:66:21)
at Object.ReactReconciler.mountComponent (/Users/aihornmac/Documents/site/ihawn/code/node_modules/react/lib/ReactReconciler.js:37:35)
at [object Object].ReactCompositeComponentMixin.mountComponent (/Users/aihornmac/Documents/site/ihawn/code/node_modules/react/lib/ReactCompositeComponent.js:225:34)
at [object Object].wrapper [as mountComponent] (/Users/aihornmac/Documents/site/ihawn/code/node_modules/react/lib/ReactPerf.js:66:21)
at Object.ReactReconciler.mountComponent (/Users/aihornmac/Documents/site/ihawn/code/node_modules/react/lib/ReactReconciler.js:37:35)
However, it was right okay on browser side. And after debugging, I found the only valid difference was that es6 classes on browser side have been transform by babel and babel-polyfill, and when I add transformation on node side, it turns out okay.
I wonder if there is anything to do with react, or just some bugs of node.js, plz help!
Can you please narrow it down to a reproducible test case and show the relevant code?
This is definitely not a bug in React as it has no control over how this is handled.
One relevant result I discovered by searching for the error message you posted is here. Apparently this is the intended ES2015 behavior. Most likely you have a class that extends another class (such as Component) but does not call super().
So if you have something like
class Blog extends Component {
constructor() {
this.state = { something: 42 };
}
}
you need to change it to call the base constructor with super():
class Blog extends Component {
constructor(props) {
super(props);
this.state = { something: 42 };
}
}
I’m closing as this cannot be caused by React itself. I hope this helps!
Thx for your help! based on your tips I found it was the es7 class properties that causes this problem, changing
class Blog extends Component {
state = {}
}
into
class Blog extends Component {
constructor (props) {
super(props);
this.state = {};
}
}
eliminates the error
btw, redux rocks!
How are you using class properties on Node? Are you also using Babel but without the class transform?
Yes, you are totally right. I just ran some tests and I think the answer is using transform-class-properties without transform-es2015-classes