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
<
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:
class SectionAccordion extends React.Component {
constructor(props){
super(props);
this.onSelect = this.onSelect.bind(this);
}
onSelect() {
this.props._onSelect(this.props.id);
}
render() {
console.log("accordion / the Accordion Section component");
var className = 'accordion-section' + (this.props._selected ? ' selected' : '');
return (
{this.props.title}
{this.props.children}
);
}
}
Update:
You can bind the context using arrow function
as well ; like this:
onSelect = () => {
// code goes here
}