javascript - How to trigger an event in React after an opened window closes - Stack Overflow

I have a div in React with an onClick handler that when clicked implements window.open in a pop-up wind

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

1 Answer 1

Reset to default 8

You 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

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信