Skip to content

Instantly share code, notes, and snippets.

@aboekhoff
Created January 15, 2017 19:14
Show Gist options
  • Save aboekhoff/f0303b8aa7186087ad8b164562444901 to your computer and use it in GitHub Desktop.
Save aboekhoff/f0303b8aa7186087ad8b164562444901 to your computer and use it in GitHub Desktop.
export function withWindowEvents(events) {
return function withWindowEventsFactory(Component) {
return class WithWindowEvents extends Component {
componentDidMount(...args) {
Object.keys(events).forEach(event => {
window.addEventListener(event, events[event])
})
super(...args)
}
componentWillUnmount(...args) {
Object.keys(events).forEach(event => {
window.removeEventListener(events[event])
})
super(...args)
}
}
}
}
/*
const withWindowClickListener = withEvents({ click: (e) => { alert('window clicked') } })
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment