admin管理员组文章数量:1302341
The following is a ponent whose functionality, partly, is to change the window's title as the page is getting focused and blurred. It does not work.
const ATitleChangingComponent = () => {
const focusFunction = (event: FocusEvent) => {
document.title = "focused";
};
const blurFunction = (event: FocusEvent) => {
document.title = "blurred";
};
useEffect(() => {
window.addEventListener("focus", focusFunction);
return window.removeEventListener("focus", focusFunction);
}, []);
useEffect(() => {
window.addEventListener("blur", blurFunction);
return window.removeEventListener("blur", blurFunction);
}, []);
return <p>some unimportant jsx</p>
};
However,
const focusFunction = (event: FocusEvent) => {
document.title = "focused";
};
window.addEventListener("focus", focusFunction);
works just fine.
A side question: are const focusFunction
and const blurFunction
getting constructed within the function each render? I assume if so, they should be lifted out of the ponent to avoid unnecessary overhead?
The following is a ponent whose functionality, partly, is to change the window's title as the page is getting focused and blurred. It does not work.
const ATitleChangingComponent = () => {
const focusFunction = (event: FocusEvent) => {
document.title = "focused";
};
const blurFunction = (event: FocusEvent) => {
document.title = "blurred";
};
useEffect(() => {
window.addEventListener("focus", focusFunction);
return window.removeEventListener("focus", focusFunction);
}, []);
useEffect(() => {
window.addEventListener("blur", blurFunction);
return window.removeEventListener("blur", blurFunction);
}, []);
return <p>some unimportant jsx</p>
};
However,
const focusFunction = (event: FocusEvent) => {
document.title = "focused";
};
window.addEventListener("focus", focusFunction);
works just fine.
A side question: are const focusFunction
and const blurFunction
getting constructed within the function each render? I assume if so, they should be lifted out of the ponent to avoid unnecessary overhead?
-
1
window.addEventListener("focus", focusFunction); return window.removeEventListener("focus", focusFunction);
<-- what is that? Why are you adding the event and removing it and returning what removeEventListener returns? – epascarello Commented Jul 29, 2019 at 22:09 -
@epascarello Returning a function that will be used to clean up once the ponent dismounts. I should return
() => window.remove...
, however this change still did not make it work. – John Smith Commented Jul 29, 2019 at 22:16
1 Answer
Reset to default 11Need to return a function, otherwise listener is removed immediately.
The function gets called when the ponent unmounts
useEffect(() => {
window.addEventListener("blur", blurFunction);
return () => window.removeEventListener("blur", blurFunction);
}, []);
本文标签: javascriptaddEventListener does not work within a useEffect hookStack Overflow
版权声明:本文标题:javascript - addEventListener does not work within a useEffect hook - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741666019a2391313.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论