-
Notifications
You must be signed in to change notification settings - Fork 322
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
Why this.setState is undefined in React ES6 class? #283
Comments
React components using ES6 classes no longer autobind
|
@troutowicz thanks! |
@troutowicz - nice one, saved me some time thanks |
@troutowicz thank you, you really save my day! |
FYI Make sure you do |
You can also use this decorator |
Disclaimer ^^ requires a polyfill and I think it doesn't work on IE8. |
@troutowicz thanks! |
@troutowicz Thanks, it works like charm! |
😊 |
You can also use the following code by combining ES6 and ES7:
onChange = (state) => {
this.setState(state);
} See React on ES6+ from Steven Luscher |
The autobinding doesn't happen with non React methods in ES6 at least. The docs led me astray, so this clarification would make them a bit more technically correct. Ideally there should be an entire sentence added at the end of the paragraph to clarify more specifically when React does and doesn't autobind to its component instance. Discovered this solution here: "React components using ES6 classes no longer autobind this to non React methods." goatslacker/alt#283
Aaaagrh.. that was killing me! Thanks a lot for this. |
👍 |
@jdlehman / @kiki-le-singe - Thank you for point the arrow syntax option which auto-binds enclosing scope this. |
thanks! |
very good. |
saved my morning |
Hi! @troutowicz your answer seemed to have helped a lot of users on this issue except me of course, now i get the following error |
Updated link: http://babeljs.io/blog/2015/06/07/react-on-es6-plus |
Thanks @kevinSuttle problem solved, Just in case anybody like me comes here you simply bind the object to your desired method like so. |
I used the arrow function to bind this to the onChange method but keep getting the 'Unexpected token' error. Even in the Steven Luscher blog post (updated link above), the code on the Arrow Function section gives same error. No sure what I'm doing wrong @troutowicz @kevinSuttle @mrbarde |
@shadrech This is phoned-in response, but I did come across this package that handles binding for you using ES7 decorators. https://github.com/andreypopp/autobind-decorator |
@kevinSuttle Thanks for the package, will try it out. |
This was super helpful. autobind came in handy when I needed to bind my methods |
none of this works for me |
@TeodorKolev why is it not working? |
or ::this.onChange ,this is more convenient. |
@TeodorKolev you could try using react autobind, works like magic.https://www.npmjs.com/package/react-autobind |
@troutowicz Thanks! Worked like a charm!! |
Hey guys I keep getting this error when using this line of code
Its not from binding I think. Any thoughts? Initially I was just trying to call My code:
|
@jasan-s If you want any method(function) within your class to work, you need to bind it, in your constructor enter the following code, |
@mrbarde thanks. I instead changed my older syntax of using function to newer arrow function () => {} |
Thank you so much, dudes. I solved my problem. |
1. Replace `React.createClass` with `React.Component` 2. Perform prop validation 3. Remove the useage of `React.createFactory`. > This helper is considered legacy, and we encourage you to either > use JSX or use `React.createElement()` directly instead. References: https://facebook.github.io/react/docs/react-api.html#createfactory goatslacker/alt#283 (comment)
In my code i got uncaught type error which shows its not defined while i have bind it within constructor. |
@troutowicz thanks |
thanx to @troutowicz This solved my problem |
I am getting TypeError: Cannot read property 'name' of undefined, when the page loads. I have registered onSubmit in the constructor pls help me |
Use Arrow function . func =( )=>{ } |
You saved my day |
Thank you |
@troutowicz your response was very helpful, thank you |
Hi, I trying to use ES6's classes for React (syntax as
class X extends React.Component
) and alt.js as flux library. When something changed in my store, my view callsonChange
, but it throws errorUncaught TypeError: this.setState is not a function
.My React component:
The text was updated successfully, but these errors were encountered: