I have a website in ReactJS. I want to get a callback whenever my tab comes in focus or is hidden. I came across the Page Visibility API for this but I\'m not able to figure out
Found this library. It could be of help... https://www.npmjs.com/package/react-page-visibility Here's how I would use it to solve your problem
import React from 'react;
import PageVisibility from 'react-page-visibility';
class YourComponent extends React.Component {
state = {
isWindowInFocus: true,
}
componentDidMount() {
const { isWindowInFocus } = this.props;
if (!isWindowInFocus) {
// do something
}
}
listentoWindow = isVisible => {
this.setState({
isWindowInFocus: isVisible,
});
}
render() {
return (
Your component JSX
);
}
}