-
Notifications
You must be signed in to change notification settings - Fork 24.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ES6-style classes & JSX binding doesn't work as expected. #1436
Comments
ES6 React classes don't autobind their methods (documentation here) so you have three options:
|
@ide nailed it, not much else to say here 😄 Awesome level of detail in your report @jaygarcia 🚀 |
@ide , thanks for the documentation. I somehow missed that. @brentvatne , thanks for closing this :) |
So far, this is the only thing that work (Lexical scoping). So, to currently use ES6-style classes with RN 0.4.4, I have to muddy up my code with adhoc binding. I don't see the value at the current time :(
|
I know it's been a while since this thread was alive, but I've had some luck by adding
It seems to work just fine for my needs, although my IDE (WebStorm 11) doesn't support the syntax yet... |
@ruswerner thanks, this is what I was looking for. |
@dropfen I'm not sure why, but it's probably because it's not finalised enough for them. Here is my
You'll need to install those packages and restart the packager before it will all work. Hope that helps! I'm on RN 0.26, which is quite different than when this thread was started. |
Hey @ruswerner! Thank you very much for this. After some research I found that there is no need to modify the just create a Hope that helps, too! |
Yep. Much better. Thanks! On Mon, Jun 27, 2016 at 10:05 AM, webdeb notifications@github.com wrote:
|
This is a fun one :)
I'm in need of class hierarchy and thought that using ES6-style classes would be the best way to achieve this result using basic ES6 OOP patterns. I've got things mostly working, but found that the JSX method binding doesn't actually act as (how I) expected and need someone to help me understand why.
The Breakdown
My class's render function returns a block of components that bind this.onButtonPress. Nothing out of the ordinary.
onButtonPress references a map to do method calls. (way cleaner than doing if/else and/or switch/case).
The method map is a plain'ol JavaScript object:
So here's the rub.
In the JSX Version, the binding works as expected this is a reference to the instance of the class.
In the ES6 version, the binding (this) points to a plain javascript object.
Here's the original class (Using React.createClass()):
https://gist.github.com/jaygarcia/e68682c284ded8e6cea0#file-gistfile1-js-L123
Here's the ES6 version.
https://gist.github.com/jaygarcia/fea5905ee361864aa5d9#file-gistfile1-js-L99
What did I do wrong? I tried to follow the Extensibility guide, but it didn't help much:
https://github.com/facebook/react-native#extensibility
/cc @grgur
The text was updated successfully, but these errors were encountered: