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

I have two React ponents. One has a window.onkeyup listener and when for instance space is pressed it p

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742372794a4431591.html

相关推荐

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

    I have two React ponents. One has a window.onkeyup listener and when for instance space is pressed it p

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信