2016-08-11 4 views
1

私は、このフォームコンポーネント持ってマテリアライズ上いいえ「のonChange」イベント:select要素

import React, { Component } from 'react' 

export default class FooForm extends Component { 
    updateFooSelection() { 
    console.log('Something should be printed... but I see nothing :('); 
    } 

    componentDidMount() { 
    this.initialiseWidgets(); 
    } 

    componentDidUpdate() { 
    this.initialiseWidgets(); 
    } 

    initialiseWidgets() { 
    Materialize.updateTextFields(); 
    $(this.refs.selectFoo).material_select(); 
    } 

    componentWillUnmount() { 
    $(this.refs.selectFoo).material_select('destroy'); 
    } 

    render() { 
    return (
     <form> 
     <select id="selFoo" ref="selectFoo" onChange={ this.updateFooSelection }> 
      <option value="foo">Foo</option> 
      <option value="bar">Bar</option> 
     </select> 
     </form> 
    ); 
    } 
}; 

しかし、機能updateFooSelectionがすべてで焼成決してです。 DOMを調べるときには、onchangeという属性などはありません。私はdocsを理解しようとしましたが、そのような要素の例はありません。

MaterializeがReactであるselect要素に対してchangeイベントを取得するにはどうすればよいですか?

どうすればonChangeイベントはこの要素に対して決して発せられませんか?

+0

の可能性のある重複[OnChangeイベント使用は、ドロップダウンのためのJSを反応させる(http://stackoverflow.com/questions/28868071/onchange-event-using-react-js-for-drop-down) – Adjit

+0

なし!このイベントは、まったくトリガーされない**です!反応的なSELECT要素を持つことではありません。 –

+0

あなたはes6を使用していますか?あなたはもう少しあなたのコンポーネントを見せることができますか? – tedcurrent

答えて

5

This issueの理由を説明します。

基本的に、ònChange小道具は単純に機能しません。代わりに.on('change', ...を使用する必要があります。

initialiseWidgets() { 
    Materialize.updateTextFields(); 
    $(this.refs.selectFoo).on('change', function() { 
    console.log('yay!'); 
    }).material_select(); 
}