私は単純なボタンホバーの例をemca6(私はbabelを使用しています)に翻訳しようとしており、失敗しています。私は私のバインドが何とか間違っていると思いますが、私は、JScriptのために新たなんだと、完全に理解していない:React - コンポーネントをecma6に翻訳するjavascript
`
constructor(props) {
super(props);`
私が意味する、私はそれがpythonでスーパーのようなものだということを得るが、なぜ奇妙パラメータとして渡された小道具の構文?
/* 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;
'this.setState =({ホバー:真});' - > 'this.setState({ホバー:真});' –
働いたおかげで、なぜ?関数に入っているので、等号なしで辞書の値を設定していますか?編集:実際には一度しか動作せず、アイドル状態に戻らず、次のホバーでタイプ未定義エラーが表示されます。 –
setStateは拡張React.componentクラスからのメソッドです。あなたは変数のように定義することはできません。私はアロンがコメントの代わりに答えを書くべきだと思う:) – mkg