I'm migrating a react app to ES6 and moving events so they're bound in the constructor, here's a very basic example:
import React from 'react';
import ReactDom from 'react-dom';
export default class Button extends React.Component {
constructor() {
super();
this.click = this.click.bind(this);
}
click() {
alert('hi');
}
render() {
return (
<button onClick={this.click}>click</button>
);
}
}
if(typeof window !== 'undefined') {
ReactDom.render(<Button/>, document.getElementById('button-spot'));
}
The button renders correctly, and there aren't any server/client errors, but when I click this button, nothing happens. I also tried console.log()
and its not logging. Am I doing the event.bind(this)
binding wrong?
more context:
my index.js router:
router.get('/', function(req, res, next) {
res.render('index', {
button: ReactDom.renderToString(<Button/>)
});
});
html page:
<div id='button-spot'><%-button%></div>
EDIT
I tried Andre's suggestion below
onClick={x => alert(1)}
and the event isn't firing, what could the issue be?