I'm trying to translate a simple button hover example to emca6 (I'm using babel) and keep failing. I guess that my bind is wrong somehow but I'm new to jscript and don't completely understand the:
`
constructor(props) {
super(props);`
I mean I get that it's like super in python, but why the weird syntax with props passed as parameter?
/* non emca6 */
import React from 'react'
var HoverButton = React.createClass({
getInitialState: function () {
return {hover: false};
},
mouseOver: function () {
this.setState({hover: true});
},
mouseOut: function () {
this.setState({hover: false});
},
render: function() {
var label = "foo";
if (this.state.hover) {
label = "bar";
}
return React.createElement(
"button",
{onMouseOver: this.mouseOver, onMouseOut: this.mouseOut},
label
);
}
});
React.render(React.createElement(HoverButton, null), document.body);
export default HoverButton;
/* emca6 */
import React from 'react';
class HoverButton extends React.Component {
constructor(props) {
super(props);
this.state = ({hover: false});
this.mouseOver = this.mouseOver.bind(this);
this.mouseOut = this.mouseOut.bind(this);
}
mouseOver(){
this.setState = ({hover: true});
}
mouseOut(){
this.setState = ({hover: false});
}
render() {
var label = "idle";
if (this.state.hover) {
label = "active";
}
return React.createElement(
"button",
{onMouseEnter: this.mouseOver, onMouseOut: this.mouseOut},
label,
);
}
}
export default HoverButton;