admin管理员组文章数量:1403226
I have a div in React with an onClick handler that when clicked implements window.open in a pop-up window that directs them to an external URL. When the user closes the pop-up window, I would like to implement an API call. However, everything I've tried has failed, including attempting to use event listeners.
How can I detect when a window is closed in React?
The current code I have for the window event is as follows:
export class App extends Component {
opener = () => {
const myWindow = window.open("", "MsgWindow", "width=200,height=100");
}
render() {
return (
<div onClick={this.opener}>
Hello World
</div>
);
}
}
I have a div in React with an onClick handler that when clicked implements window.open in a pop-up window that directs them to an external URL. When the user closes the pop-up window, I would like to implement an API call. However, everything I've tried has failed, including attempting to use event listeners.
How can I detect when a window is closed in React?
The current code I have for the window event is as follows:
export class App extends Component {
opener = () => {
const myWindow = window.open("https://www.espn.", "MsgWindow", "width=200,height=100");
}
render() {
return (
<div onClick={this.opener}>
Hello World
</div>
);
}
}
Share
Improve this question
asked Dec 5, 2018 at 4:56
DogDog
2,9167 gold badges34 silver badges72 bronze badges
1 Answer
Reset to default 8You could change the opener
function as below:
opener = () => {
const myWindow = window.open("https://www.espn.", "MsgWindow", "width=200,height=100");
var timer = setInterval(function() {
if(myWindow.closed) {
clearInterval(timer);
alert('closed');
}
}, 1000);
}
Reference | Codepen
本文标签: javascriptHow to trigger an event in React after an opened window closesStack Overflow
版权声明:本文标题:javascript - How to trigger an event in React after an opened window closes - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744333890a2601092.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论