javascript React:如何监听子组件事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28887570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 09:42:43  来源:igfitidea点击:

React: How to listen to child component events

javascriptreactjs

提问by Simon

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? 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.

我的问题是;如何从父(表单)组件中为选择菜单挂接到 onChange 事件?我无法通过道具传递 onChange ,因为我已经在 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>
        );
    }
});

采纳答案by Jordan Running

Using <Select onChange={...} />won't override the <select onChange={...} />inside Select's rendermethod. The <Select/>component and the <select/>component it renders have completely different sets of props.

使用<Select onChange={...} />不会覆盖<select onChange={...} />内部 Select 的render方法。该<Select/>组件和<select/>组件它呈现具有完全不同的套props

The simplest way to do what you want, I think, is to have your Select's handleChangemethod call this.props.onChange. You can just pass it the same eargument handleChangereceives:

我认为,做你想做的最简单的方法是让你的 SelecthandleChange方法调用this.props.onChange。您可以将其传递给相同的e参数handleChange

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>
    );
  }
});