最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Stop event propagation from input in React - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 4

You 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");
    }
  }
}

发布评论

评论列表(0)

  1. 暂无评论