Passing an additional parameter with an onChange event

前端 未结 7 833
生来不讨喜
生来不讨喜 2021-01-31 07:33

What I\'m trying to do:

I am trying to pass a string from a child component to the handleChange function of a parent component.

What cur

7条回答
  •  天涯浪人
    2021-01-31 07:47

    In order to pass a param from the child component to the parent you can take an argument to the arrow function.

    handleChange(event, section) {
        console.log(section);
        console.log(event.target.value);
    }
     this.handleChange(e, val)} />
    
    
    this.props.handleChange(e, "tags")}>
    Tag 1:
    Tag 2:
    Tag 3:

    Sample snippet

    class App extends React.Component {
      handleChange(e, val) {
        console.log(e.target.value, val);
      }
      render() {
        return(
           this.handleChange(e, val)}/>
        )
      }
    }
    
    class Child extends React.Component {
      
      render() {
        return(
           this.props.handleChange(e, 'tab')}/>
        )
      }
    }
    
    ReactDOM.render(, document.getElementById('app'));
    
    
    

提交回复
热议问题