admin管理员组文章数量:1024615
I have a button with onClick
event attached to it:
<button id="my-button" onClick={myMethod}>
My button
</button>
I have also added an event listener to this button:
const listener = (e) => {
// Do something here (or elsewhere) to prevent `myMethod` from being invoked
console.log('Hello, world!');
}
const options = { capture: true, once: true };
document.getElementById('my-button')
.addEventListener('click', listener, options);
Is it possible to add some method inside the listener
, so the myMethod
is stopped from being invoked?
I have a button with onClick
event attached to it:
<button id="my-button" onClick={myMethod}>
My button
</button>
I have also added an event listener to this button:
const listener = (e) => {
// Do something here (or elsewhere) to prevent `myMethod` from being invoked
console.log('Hello, world!');
}
const options = { capture: true, once: true };
document.getElementById('my-button')
.addEventListener('click', listener, options);
Is it possible to add some method inside the listener
, so the myMethod
is stopped from being invoked?
-
have you tried
event.preventDefault();
? – Phelipe Rocha Commented Sep 2, 2018 at 12:18 -
Is
myMethod
your code? – Anthony Commented Sep 2, 2018 at 12:18 - 3 you should not mix react and plain js handlers. that just creates chaos. – Jonas Wilms Commented Sep 2, 2018 at 12:19
- 2 @AbSin - reactjs/docs/handling-events.html – T.J. Crowder Commented Sep 2, 2018 at 12:23
-
1
@abSin Cause
onClick
is part of React<button />
s props, it doesnt get reflected to the DOM. – Jonas Wilms Commented Sep 2, 2018 at 12:24
1 Answer
Reset to default 8Combining React event handling and raw DOM event handling usually indicates a larger design issue. Having the one conflict with the other even more so. :-)
Having said that, React's event handlers use delegation, so the standard e.stopPropagation
should do it:
const listener = (e) => {
e.stopPropagation();
console.log('Hello, world!');
};
Example:
function myMethod() {
console.log("myMethod");
}
const Example = () => <button id="my-button" onClick={myMethod}>
My button
</button>;
ReactDOM.render(
<Example />,
document.getElementById("root")
);
const listener = (e) => {
// Do something here (or elsewhere) to prevent `myMethod` from being invoked
e.stopPropagation();
console.log('Hello, world!');
}
const options = { capture: true, once: true };
document.getElementById('my-button')
.addEventListener('click', listener, options);
<div id="root"></div>
<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>
Note that you'll need to re-attach your event handler every time React re-renders the ponent. This is part of why mixing these two systems is generally not your best approach.
I have a button with onClick
event attached to it:
<button id="my-button" onClick={myMethod}>
My button
</button>
I have also added an event listener to this button:
const listener = (e) => {
// Do something here (or elsewhere) to prevent `myMethod` from being invoked
console.log('Hello, world!');
}
const options = { capture: true, once: true };
document.getElementById('my-button')
.addEventListener('click', listener, options);
Is it possible to add some method inside the listener
, so the myMethod
is stopped from being invoked?
I have a button with onClick
event attached to it:
<button id="my-button" onClick={myMethod}>
My button
</button>
I have also added an event listener to this button:
const listener = (e) => {
// Do something here (or elsewhere) to prevent `myMethod` from being invoked
console.log('Hello, world!');
}
const options = { capture: true, once: true };
document.getElementById('my-button')
.addEventListener('click', listener, options);
Is it possible to add some method inside the listener
, so the myMethod
is stopped from being invoked?
-
have you tried
event.preventDefault();
? – Phelipe Rocha Commented Sep 2, 2018 at 12:18 -
Is
myMethod
your code? – Anthony Commented Sep 2, 2018 at 12:18 - 3 you should not mix react and plain js handlers. that just creates chaos. – Jonas Wilms Commented Sep 2, 2018 at 12:19
- 2 @AbSin - reactjs/docs/handling-events.html – T.J. Crowder Commented Sep 2, 2018 at 12:23
-
1
@abSin Cause
onClick
is part of React<button />
s props, it doesnt get reflected to the DOM. – Jonas Wilms Commented Sep 2, 2018 at 12:24
1 Answer
Reset to default 8Combining React event handling and raw DOM event handling usually indicates a larger design issue. Having the one conflict with the other even more so. :-)
Having said that, React's event handlers use delegation, so the standard e.stopPropagation
should do it:
const listener = (e) => {
e.stopPropagation();
console.log('Hello, world!');
};
Example:
function myMethod() {
console.log("myMethod");
}
const Example = () => <button id="my-button" onClick={myMethod}>
My button
</button>;
ReactDOM.render(
<Example />,
document.getElementById("root")
);
const listener = (e) => {
// Do something here (or elsewhere) to prevent `myMethod` from being invoked
e.stopPropagation();
console.log('Hello, world!');
}
const options = { capture: true, once: true };
document.getElementById('my-button')
.addEventListener('click', listener, options);
<div id="root"></div>
<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>
Note that you'll need to re-attach your event handler every time React re-renders the ponent. This is part of why mixing these two systems is generally not your best approach.
本文标签: javascriptBlocking onClick handler with addEventListenerStack Overflow
版权声明:本文标题:javascript - Blocking onClick handler with addEventListener - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745610273a2158962.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论