Skip to content

Instantly share code, notes, and snippets.

@mrowa44
Created August 12, 2016 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrowa44/cf14a1966ee566141dd65d890077b6bf to your computer and use it in GitHub Desktop.
Save mrowa44/cf14a1966ee566141dd65d890077b6bf to your computer and use it in GitHub Desktop.
function WindowListener(windowEvents, windowHandler, capture = false) {
const events = (typeof windowEvents === 'string') ? [windowEvents] : windowEvents;
const handler = windowHandler || 'forceUpdate';
return {
componentDidMount() {
for (let i = 0; i < events.length; i++) {
window.addEventListener(events[i], this[handler], capture);
}
},
componentWillUnmount() {
for (let i = 0; i < events.length; i++) {
window.removeEventListener(events[i], this[handler], capture);
}
},
};
}
export default WindowListener;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment