onChange回调没有在React组件中触发

时间:2015-10-31 22:17:15

标签: javascript jquery reactjs

除非我出于某种原因使用jQuery,否则我的onChange()函数不会运行。我必须在onChange中添加componentDidMount()侦听器的原因是因为我使用MaterializeCSS将select标记转换为ul。下面的代码工作正常:

onChange(e) {
  let inspectionTime = e.target.value;
  this.setState({ inspectionTime });
}

componentDidMount() {
  let $inspectionDropdown = $(ReactDOM.findDOMNode(this.refs.inspection));
  $inspectionDropdown.on('change', this.onChange);
}

但是这段代码没有:

onChange(e) {
  let inspectionTime = e.target.value;
  this.setState({ inspectionTime });
}

componentDidMount() {
  let inspectionDropdown = ReactDOM.findDOMNode(this.refs.inspection);
  inspectionDropdown.addEventListener('change', this.onChange);
}

以下是整个组件的代码,如果它有帮助的话:

import React from 'react';
import ReactDOM from 'react-dom';

class InspectionMode extends React.Component {
  constructor(props) {
    super(props);
    this.onChange = this.onChange.bind(this);
    this.defaultValue = 'selected';
    this.state = { inspectionTime: 0 };
  }

  onChange(e) {
    let inspectionTime = e.target.value;
    this.setState({ inspectionTime });
  }

  componentDidMount() {
    let inspectionDropdown = ReactDOM.findDOMNode(this.refs.inspection);
    inspectionDropdown.addEventListener('change', this.onChange);
  }

  render() {
    let classes = 'input-field col s10 offset-s1 l3';

    return (
      <div className={classes}>
        <select onChange={this.onChange} ref="inspection" value={this.state.inspectionTime}>
          <option value="0">None</option>
          <option value='5'>5 Seconds</option>
          <option value='10'>10 Seconds</option>
          <option value='15'>15 Seconds</option>
        </select>
        <label>Inspection Time</label>
      </div>
    );
  }
}

export default InspectionMode;

1 个答案:

答案 0 :(得分:1)

问题是该插件正在发出自定义更改事件。 React的事件系统无法识别自定义事件(不确定原因)。

在这种情况下,手动更改侦听器是正确的解决方案。改进方法的方法是将select元素抽象为&#39;原子&#39;。

class Select extends React.Component {
  static propTypes = {
    value: React.PropTypes.string,
    options: React.PropTypes.arrayOf(
      React.PropTypes.shape({
        text: React.PropTypes.string.isRequired,
        value: React.PropTypes.string.isRequired,
      })
    ).isRequired
  };
  constructor() {
    super();
    this.onChange = this.onChange.bind(this);
  }

  onChange(e) {
    this.props.onChange(e.target.value);
  }

  componentDidMount() {
    let select = this.refs.select;
    select.addEventListener('change', this.onChange, false);
  }

  componentWillUnmount(){
    let select = this.refs.select;
    select.removeEventListener('change', this.onChange, false);  
  }

  render() {
    let classes = 'input-field col s10 offset-s1 l3';

    return (
      <div className={classes}>
        <select ref="select" value={this.props.value}>
          {this.props.options.map((x) => {
            return <option key={x.value} value={x.value}>{x.text}</option>;
          })}
        </select>
      </div>
    );
  }
}

然后,您可以在InspectionMode或UI中的任何其他位置使用此功能。

class InspectionMode extends React.Component {
  constructor(props) {
    super(props);
    this.onChange = this.onChange.bind(this);
    this.value = 'selected';
    this.state = { inspectionTime: 0 };
  }

  onChange(inspectionTime) {
    this.setState({ inspectionTime });
  }

  render() {
    return (
      <div>
        <Select 
          value={this.state.inspectionTime}
          onChange={this.onChange}
          options={[
            {value: '0', text: 'None'},
            {value: '5', text: '5 seconds'},
            {value: '10', text: '10 seconds'},
            {value: '15', text: '15 seconds'},
          ]}
        />
        <label>Inspection Time</label>
      </div>
    );
  }
}