2016-06-17 6 views
0

カスタムReactボタンのclickイベントにlegitRipple.jsを追加します。 legitRipple JSへ リンク:ここhttps://matthias-vogt.github.io/legitRipple.js/Reactカスタムコンポーネント - ボタン - OnClickイベントでJQueryプラグインを呼び出します。

は私の案ですが、それが動作していない - 私は(.rippleするクリックイベントや参照をリンクする方法がわからない)イベント

Button = class Button extends React.Component { 
    constructor(props) { 
    super(props); 
    this.onClick = this.onClick.bind(this); 
    this.state = { 

    }; 
    } 
    componentDidMount() { 
    //need to add JQuery function - on button click, I need to register an event? or somehow call (ref=buttonEl) $(buttonEl).ripple(); 

    } 

//do I need an componentWillUnmount as well? 

    onClick(ev) { 
    let onClickFn = this.props.onClick; 
    onClickFn && onClickFn(ev); 
    //somehow register an event to show the ripple? 
    } 
    render() { 
    return (
     <div> 
     <button type="button" ref="buttonEl" className={this.props.className} onClick={this.onClick.bind(this)}> 
      {this.props.children} 
     </button> 
     </div> 
    ); 
    } 
} 

答えて

0

することができますthis.refs.buttonElを使用してボタン要素にアクセスし、$(this.refs.buttonEl).ripple()を実行してその要素にリップルハンドラをインストールできるようにする必要があります。

ドキュメントからは、クリックすると要素が自動的に波打つように見えます。それを実現させるためにonClick中に何かを呼び出さなければならないように見えません。

componentWillUnmountには$(this.refs.buttonEl).ripple({unbind: true});を設定します。また、componentWillUpdateの間にイベントハンドラを削除し、componentDidUpdateの間にそれを添付する必要があります。

+0

ありがとうございました。私は、jQueryを呼び出すためにonClickイベントを待つ必要のないドキュメントを読んでいたはずです。 私はこれが問題ではないことを知っていますが、クリックイベントでjqueryプラグインを実行する必要があった場合は、どうすればよいでしょうか?私は将来この問題にぶつかるImを知っている。ほぼ同じように – hgill

+0

。あなたのonClickでは、同じthis.refs.buttonElを使用して要素を取得して、普通の古いJSの中で何かjQueryを呼び出すことができます。また、コンストラクタでバインドしているので、onClick = {this.onClick} – Ryan

関連する問題