简体   繁体   English

React:如何监听子组件事件

[英]React: How to listen to child component events

I have a component, let's say it contains a form. 我有一个组件,假设它包含一个表单。 The form has child components which are essentially UI widgets for outputting text inputs and select menus. 该表单具有子组件,它们本质上是用于输出文本输入和选择菜单的UI小部件。

The select menu components are a bit fancy and do some state maintaining using the onChange event. 选择菜单组件有点花哨,并使用onChange事件进行一些状态维护。

My question is; 我的问题是; how do I hook into the onChange event for a select menu from the parent (form) component? 如何从父(表单)组件挂钩到选择菜单的onChange事件? I can't pass onChange through props as I already have onChange specified inside the select component and I don't want to override it. 我无法通过道具传递更改,因为我已经在select组件中指定了onChange并且我不想覆盖它。

Example: 例:

var Form = React.createClass({

    handleSelectChange: function(){
        // Do something when <Select /> changes
    },

    render: function () {    

        var selectMenuOptions = [
            {label: 'Choose...', value: ''},
            {label: 'First option', value: 'one'},
            {label: 'Second option', value: 'two'}
        ];
        return (
            <form>
                <Select name="selectMenu" id="selectMenu" options={selectMenuOptions} />
            </form>
          );
        }
});

var Select = React.createClass({

    getDefaultProps: function() {
        return {
          options: [],
          className: "select"
        };
      },

    getInitialState: function () {
        return {
            buttonText: 'Loading...',
            defaultValue: null 
        };
    },

    handleChange: function (e) {
        // Update buttonText state
    },

    render: function () {

        return (
            <div className={this.props.className}>
                <div className="select__button">{this.state.buttonText}</div>
                <select className="select__selector" 
                        ref="select" 
                        onChange={this.handleChange}>
                        {this.props.options.map(function(option, i){
                            return (<Option option={option} key={i} />);
                        })}
                </select>
            </div>
        );
    }
});

Using <Select onChange={...} /> won't override the <select onChange={...} /> inside Select's render method. 使用<Select onChange={...} />不会覆盖Select的render方法中的<select onChange={...} /> The <Select/> component and the <select/> component it renders have completely different sets of props . <Select/>组件和它呈现的<select/>组件具有完全不同的props

The simplest way to do what you want, I think, is to have your Select's handleChange method call this.props.onChange . 我认为,最简单的方法就是让你的Select的handleChange方法调用this.props.onChange You can just pass it the same e argument handleChange receives: 您可以将它传递给handleChange收到的相同e参数:

var Form = React.createClass({
  handleSelectChange: function(){
    // Do something when <Select /> changes
  },

  render: function () {
    // ...
    return (
      <form>
        <Select name="selectMenu"
          id="selectMenu"
          options={selectMenuOptions}
          onChange={this.handleSelectChange} />
      </form>
      );
    }
});

var Select = React.createClass({
  // ...

  handleChange: function (e) {
    if (this.props.onChange) {
      this.props.onChange(e);
    }
    // Update buttonText state
  },

  render: function () {
    return (
      <div className={this.props.className}>
        <div className="select__button">{this.state.buttonText}</div>
        <select className="select__selector"
          ref="select"
          onChange={this.handleChange}>
          {this.props.options.map(function(option, i){
            return (<Option option={option} key={i} />);
          })}
        </select>
      </div>
    );
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM