简体   繁体   English

处理好onClick on ReactJS

[英]Handle good onClick on ReactJS

I have a full view with an onClick handler and a button inside this view with its own onClick handler. 我有一个带有onClick处理程序的完整视图,并且该视图内有一个带有自己的onClick处理程序的按钮。 I want the onclick bound to the view to fire when I click anywhere except on the button. 我希望在单击按钮上除按钮之外的任何位置时触发绑定到视图的onclick。 I want the onclick bound to the button to fire only when the button is clicked. 我希望绑定到按钮的onclick仅在单击按钮时才触发。

onClick(event){
    event.stopPropagation();
    this.props.onClick(this.props.item)
}

onDeleteClick(){

}

render(){

    const paper = {
        height: 180,
        width: 400,
        backgroundColor : "#ECEFF1",
        display : "flex",
        flexDirection : "column",
        cursor: this.state.cursor
    }


    const littlePartStyle = {
        width : "100%", height : 40, display : "flex", alignItems : "center", marginLeft : 30
    }


    return(
        <Paper onClick={(event) => {this.onClick(event)}} onMouseLeave={() => {this.onMouseLeft()}} onMouseOver={() => {this.onMouseOver()}} style={paper} >
                <div  style={{width : "100%", height : "30%", alignItems : "center",  display : "flex"}}>
                    <div style={{display : "flex", marginLeft : 30, width : "50%", fontSize : 20}}>{this.props.item.name}</div>
                    <div style={{width : "50%"}}>
                        <Button onClick={() => {this.onDeleteClick()}} style={{backgroundColor : "black", color : "white", marginLeft : 70, overflow : "hidden"}}>Delete</Button>
                    </div>
                </div>
            <div style={{width : "100%", height : "70%", display : "flex", flexDirection : "column"}}>
                <div style={littlePartStyle}>
                    <div>Session organizer : {this.props.item.organizer}</div>
                </div>
                <div style={{width : "80%", height : 1,  backgroundColor : "black", marginLeft : 30}}>
                </div>
                <div style={littlePartStyle}>
                    <div>Session subject : {this.props.item.subject}</div>
                </div>
                <div style={{width : "80%", height : 1,  backgroundColor : "black", marginLeft : 30}}>
                </div>

                <div style={littlePartStyle}>
                    <div>Creation date : {this.props.item.creationDate}</div>
                </div>


            </div>
        </Paper>
    )
}

Just pass the event to the onDelete handler and stop the propagation: 只需将事件传递给onDelete处理程序并停止传播即可:

onClick(event){
    event.stopPropagation();
    this.props.onClick(this.props.item)
}

onDeleteClick(event){
    event.stopPropagation();
    //do something
}

render() {
    return (
        <Paper onClick={ e => this.onClick(e) }>
           ///
           <Button onClick={ e => this.onDeleteClick(e) } >Your button</Button>
           ///
        </Paper>
    )
}

In this case, you need to install another npm module called react-onclickoutside Make separate two component like FullViewComponent as you want and InnerButtonComponent . 在这种情况下,您需要安装另一个npm模块,称为react-onclickoutside 。根据需要InnerButtonComponent两个单独的组件,例如FullViewComponentInnerButtonComponent render your view with InnerButtonComponent . 使用InnerButtonComponent渲染视图。 Then in your 然后在你的
InnerButtonComponent.js InnerButtonComponent.js

import React from 'react';
import onClickOutside from 'react-onclickoutside';

class InnerButtonComponent extends React.Component {
   constructor(props) {
     super(props);

   }
   handleClickOutside (e) {
    // outside click staff here
    console.log(e);
  }

  render () {
    return (
       <div>....</div>
    );
  }
}
export default onClickOutside(InnerButtonComponent);

that's it. 而已。

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

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