我有一个子组件在父组件内。我想要当我点击父级组件除子组件之外的任意地方时弹出消息。
class TodoApp extends React.Component {
constructor(props) {
super(props)
this.state = {
}
}
render() {
return (
<div className="parent" onClick={() => alert('x')}>
<p>Alert clicks anywhere on this div</p>
<div className="children">
<button>Do not alert On this element click</button>
</div>
<p>Alert clicks anywhere on this div</p>
</div>
)
}
}
以下为代码示例: