React: How to listen to child component events

后端 未结 1 1411
日久生厌
日久生厌 2021-02-05 22:54

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.

The select

1条回答
  •  闹比i
    闹比i (楼主)
    2021-02-05 23:29

    Using inside Select's render method. The component it renders have completely different sets of props.

    The simplest way to do what you want, I think, is to have your Select's handleChange method call this.props.onChange. You can just pass it the same e argument handleChange receives:

    var Form = React.createClass({
      handleSelectChange: function(){
        // Do something when 
          
          );
        }
    });
    
    var Select = React.createClass({
      // ...
    
      handleChange: function (e) {
        if (this.props.onChange) {
          this.props.onChange(e);
        }
        // Update buttonText state
      },
    
      render: function () {
        return (
          
    {this.state.buttonText}
    ); } });

    0 讨论(0)
提交回复
热议问题