admin管理员组文章数量:1336422
I have two React ponents. One has a window.onkeyup
listener and when for instance space is pressed it performs an action. The other has input elements to edit text attributes. However, when the text is edited in the second ponent the keyevent
is also fired in the first ponent. I have tried adding:
event.stopPropagation()
event.preventDefault()
event.nativeEvent.stopImmediatePropagation()
In the event handler but none of these seems to stop the event.
I made a code example, in which case I don't want the window.onkeyup
event to fire when typing in the input. Is there any way to solve this?
class Hello extends React.Component {
ponentDidMount() {
window.onkeyup = () => console.log("hello")
}
handleChange(event) {
event.preventDefault()
event.nativeEvent.stopImmediatePropagation()
}
render() {
return <div onChange={this.handleChange}><input ></input></div>
}
}
ReactDOM.render(
<Hello name="World" />,
document.getElementById('container')
);
<script src=".1.0/react.min.js"></script>
<script src=".1.0/react-dom.min.js"></script>
<div id="container"></div>
I have two React ponents. One has a window.onkeyup
listener and when for instance space is pressed it performs an action. The other has input elements to edit text attributes. However, when the text is edited in the second ponent the keyevent
is also fired in the first ponent. I have tried adding:
event.stopPropagation()
event.preventDefault()
event.nativeEvent.stopImmediatePropagation()
In the event handler but none of these seems to stop the event.
I made a code example, in which case I don't want the window.onkeyup
event to fire when typing in the input. Is there any way to solve this?
class Hello extends React.Component {
ponentDidMount() {
window.onkeyup = () => console.log("hello")
}
handleChange(event) {
event.preventDefault()
event.nativeEvent.stopImmediatePropagation()
}
render() {
return <div onChange={this.handleChange}><input ></input></div>
}
}
ReactDOM.render(
<Hello name="World" />,
document.getElementById('container')
);
<script src="https://cdnjs.cloudflare./ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="container"></div>
Share
Improve this question
edited Jan 8, 2021 at 8:57
Brian Tompsett - 汤莱恩
5,89372 gold badges61 silver badges133 bronze badges
asked Aug 10, 2018 at 22:09
AlexVestinAlexVestin
2,5862 gold badges18 silver badges20 bronze badges
2 Answers
Reset to default 4You want to use the onKeyUp
event instead, and use event.stopPropagation
.
class Hello extends React.Component {
ponentDidMount() {
window.onkeyup = () => console.log("hello");
}
handleKeyUp(event) {
event.stopPropagation();
}
render() {
return <div onKeyUp={this.handleKeyUp}><input /></div>;
}
}
ReactDOM.render(<Hello name="World" />, document.getElementById("container"));
<script src="https://cdnjs.cloudflare./ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="container"></div>
The solution for a similar case where I had an event listener attached to the space button, with input elements on the same page:
In the event listener, if focus is on an input element, return
:
const listener = (e) => {
if (e.target.tagName === "INPUT" || e.target.tagName === "BUTTON"){
return
} else {
if(e.key === " "){
console.log("Do something");
}
}
}
本文标签: javascriptStop event propagation from input in ReactStack Overflow
版权声明:本文标题:javascript - Stop event propagation from input in React - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742372794a2462564.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论