Created
January 15, 2017 19:14
-
-
Save aboekhoff/f0303b8aa7186087ad8b164562444901 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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