admin管理员组文章数量:1287778
I'm sorry if I duplicate the question but I didn't find any solution for my problem.
What is the best way in React to detect switching tabs in browser or hide browser window?
I know there is a Page visibility API for it but how can I implement it in React ponent?
Here is the easiest way but I don't know is correct
import React, { Component } from 'react';
class Sample extends Component {
handleBlur = () => {
console.log('Blur');
}
handleFocus = () => {
console.log('Focus');
}
render() {
return (
<div
style={{ width: 400, height: 200 }}
onBlur={this.handleBlur}
onFocus={this.handleFocus}
>
test
</div>
);
}
}
export default Sample;
I'm sorry if I duplicate the question but I didn't find any solution for my problem.
What is the best way in React to detect switching tabs in browser or hide browser window?
I know there is a Page visibility API for it but how can I implement it in React ponent?
Here is the easiest way but I don't know is correct
import React, { Component } from 'react';
class Sample extends Component {
handleBlur = () => {
console.log('Blur');
}
handleFocus = () => {
console.log('Focus');
}
render() {
return (
<div
style={{ width: 400, height: 200 }}
onBlur={this.handleBlur}
onFocus={this.handleFocus}
>
test
</div>
);
}
}
export default Sample;
Share
Improve this question
edited Oct 10, 2018 at 16:44
hofshteyn
asked Oct 10, 2018 at 16:28
hofshteynhofshteyn
1,2724 gold badges17 silver badges34 bronze badges
2
- What have you tried? Could you provide us with some code? – zfrisch Commented Oct 10, 2018 at 16:31
- @zfrisch added a sample – hofshteyn Commented Oct 10, 2018 at 16:45
1 Answer
Reset to default 8let hidden = null;
let visibilityChange = null;
if (typeof document.hidden !== 'undefined') { // Opera 12.10 and Firefox 18 and later support
hidden = 'hidden';
visibilityChange = 'visibilitychange';
} else if (typeof document.msHidden !== 'undefined') {
hidden = 'msHidden';
visibilityChange = 'msvisibilitychange';
} else if (typeof document.webkitHidden !== 'undefined') {
hidden = 'webkitHidden';
visibilityChange = 'webkitvisibilitychange';
}
class Hello extends React.Component {
state = {
actions: []
}
ponentDidMount() {
document.addEventListener(visibilityChange, this.handleVisibilityChange, false);
}
handleVisibilityChange = () => {
if (document[hidden]) {
this.setState({actions: [...this.state.actions, 'hide']});
} else {
this.setState({actions: [...this.state.actions, 'show']});
}
}
ponentWillUnmount() {
document.removeEventListener(visibilityChange, this.handleVisibilityChange);
}
render() {
return (
<ul>
{
this.state.actions.map((item, key) => <li key={key}>{item}</li>)
}
</ul>
)
}
}
ReactDOM.render(
<Hello />,
document.getElementById('container')
);
本文标签: javascriptReact JS ability to detect switching tab in browserStack Overflow
版权声明:本文标题:javascript - React JS: ability to detect switching tab in browser - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741328056a2372603.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论