React - Prevent Event Trigger on Parent From Child

前端 未结 3 1291
眼角桃花
眼角桃花 2021-02-01 00:52

I have this scenario, where when parent element is clicked, it flips to show a child element with different colours. Unfortunately, when the user clicks on one of the colours, t

3条回答
  •  情深已故
    2021-02-01 01:10

    I wanted to invoke function on props but at the same time wanted to stop event propagation from child to parent, here is how its handled

    class LabelCancelable extends Component {
    
      handleChildClick(e) {
        e.stopPropagation()
      }
      closeClicked(e, props) {
        e.stopPropagation();
        props.onCloseClicked()
      }
    
      render() {
        const {displayLabel} = this.props;
        return (
          
              
              
                { displayLabel }
              
          
        );
      }
    }
    
    export default LabelCancelable;
    

提交回复
热议问题