我一直在React的活动系统的舒适范围内探索一些事件处理,并了解它如何与外部' DOM中的世界。
这是我正在试验的注释样本:
class FunWithEvents extends Component {
handleKeyUpWithoutReact(e) {
console.warn('OMG I handled an event without React ');
// I noticed that `e.cancelBubble` is `true` here...
}
handleKeyUpParent(e) {
// This handler appears to be under React's supervision...
console.info('I get prevented :)');
}
handleKeyUpTarget(e) {
console.info("I'm shown in the console :)");
e.stopPropagation();
}
componentDidMount() {
document.addEventListener('keyup', this.handleKeyUpWithoutReact);
}
componentWillUnmount() {
document.removeEventListener('keyup', this.handleKeyUpWithoutReact);
}
render() {
return (
<div onKeyUp={(e) => this.handleKeyUpParent(e)}>
<input onKeyUp={(e) => this.handleKeyUpTarget(e)} />
</div>
);
}
}
正如支持的注释所希望的那样,我注意到了:
input
开始,确实按照我的预期触发了handleKeyUpTarget
处理程序。e.stopPropagation
内的handleKeyUpTarget
防止进一步冒泡。handleKeyUpParent
处理程序按照我的预期不运行。handleKeyUpWithoutReact
会被调用。这让我措手不及,正确或(可能)错误。e.stopPropagation()
冒泡的本机事件?这是设计用于与共享环境的互操作性吗?cancelBubble
设置为true
,如handleKeyUpWithoutReact
方法所示?提前致谢!
答案 0 :(得分:1)
event.stopPropagation()
仅停止传播到React中附加的事件。例如,onKeyUp
,onChange
,onClick
等上的事件都是React中的合成事件,并且为了使用document.addEventListener
附加的外部事件停止传播,您需要使用nativeEvent并运行e.nativeEvent.stopImmediatePropagation();
class FunWithEvents extends React.Component {
handleKeyUpWithoutReact(e) {
console.warn("OMG I handled an event without React ");
// I noticed that `e.cancelBubble` is `true` here...
}
handleKeyUpParent(e) {
// This handler appears to be under React's supervision...
console.info("I get prevented :)");
}
handleKeyUpTarget(e) {
console.info("I'm shown in the console :)");
e.stopPropagation();
e.nativeEvent.stopImmediatePropagation();
}
componentDidMount() {
document.addEventListener("keyup", this.handleKeyUpWithoutReact);
}
componentWillUnmount() {
document.removeEventListener("keyup", this.handleKeyUpWithoutReact);
}
render() {
return (
<div onKeyUp={e => this.handleKeyUpParent(e)}>
<input onKeyUp={e => this.handleKeyUpTarget(e)} />
</div>
);
}
}
<强> Working DEMO 强>