I am trying to change from React.createClass to React.Component, but I am getting below error.
Uncaught TypeError: Cannot read property 'state' of undefined
I googled for the error but an not able to figure it out
class Accordion extends React.Component {
You need to bind
this.onSelect
If you forget to bind this.onSelect and pass it to onClick, this will be undefined when the function is actually called.Try this:
Update:
You can bind the context using
arrow function
as well ; like this:You need to bind your
onSelect
to the class in the constructor:The
extends
syntax no longer has autobinding likecreateClass
did.You need to bind
this
.this.onSelect -> this.onSelect.bind(this)
this.enhanceSection -> this.enhanceSection.bind(this)