2017-01-21 9 views
0

この問題について多くのスレッドを読んだことがありますが、修正できません。私は何もhappendsないスパンをクリックしてくださいReactJS onClickが動作しません - ブラウザとシステムコンソールで何も起こりません

/** @jsx React.DOM */ 

var React = require('react/addons'); 

var SegmentComponent = React.createClass({ 

    handleThatEvent: function (e) 
    { 
     console.log('clicked'); 
    }, 

    render: function() 
    { 
     const styles = { 
      left: this.props.leftPercent + '%', 
      width: this.props.widthPercent + '%' 
     }; 

     if (this.props.color !== undefined) 
     { 
      styles.backgroundColor = this.props.color; 
     } 

     return (
      <span onClick={this.handleThatEvent()} style={styles} className="track-notation"></span> 
     ) 
    } 
}); 

module.exports = SegmentComponent; 

。私はReactJSで新しく、多分私は明白なことを逃した。

ウェブブラウザのコンソールとシステムコンソール(端末)に「クリックされた」テキストはありません。

EDIT:

私は

onClick={this.handleThatEvent.bind(this)} 

{() => this.handleThatEvent()} 

で試しても何もありません。また

HTMLでのonClick span要素であっ属性ではありません。

onClick={this.handleThatEvent()} 

は、そのようにする必要があります:問題です

<span style="left:10%;width:10%;" class="track-notation" data-reactid=".rru3h8zkm2.1:0.0.2.0"></span> 

答えて

0

onClick={this.handleThatEvent.bind(this)} 

または

onClick={() => this.handleThatEvent()} 
+0

それは動作していません - 私は質問を更新しました – Simon

+0

React.creatClassは自動バインドを行います –

+0

@シモンあなたはこれを試してください:onclick = 'handleThatEvent()' ..確かではない..またはonclick = 'handleThatEvent' ...または試してみてください

1
onClick={this.handleThatEvent()} 

置き換える

onClick={this.handleThatEvent} 
+0

これは機能しません。 "onClick"属性はスパン要素にありません – Simon

+0

Simonはイベントの反応文書を読んでいますか? https://facebook.github.io/react/docs/events.html。 onClickはそれがspan要素に決して反応することはない、onClickはDOMに存在しない。 –

+0

ここを見てください - > http://jsbin.com/gekolufixo –

1

慎重にあなたはonClickイベントにthis.handleThatEvent()を通過し、そして、あなたが参照してください...それを実行しますonClickにのみ機能this.handleThatEventを渡す必要があり、それを実行しました:

const SegmentComponent = React.createClass({ 
 
    handleThatEvent: function (e) { 
 
    console.log('clicked'); 
 
    }, 
 
    render: function() { 
 
    const styles = { 
 
     left : this.props.leftPercent + '%', 
 
     width : this.props.widthPercent + '%' 
 
    }; 
 
    if (this.props.color !== undefined) { 
 
     styles.backgroundColor = this.props.color; 
 
    } 
 
    return (
 
     <span 
 
     onClick={this.handleThatEvent} 
 
     style={styles} 
 
     className="track-notation" 
 
     >Click me! 
 
     </span> 
 
    ) 
 
    } 
 
}); 
 

 
ReactDOM.render(
 
    <SegmentComponent />, 
 
    document.getElementById('root') 
 
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> 
 
<div id="root"></div>

+0

それは動作していません:( – Simon

+0

私はライブ作業の例を追加しました...あなたは別の問題があります –

+0

@シモンおそらくあなたはJavascriptコンパイラを使用しないでくださいバベルのように –

関連する問題