Skip to content

Instantly share code, notes, and snippets.

@barneycarroll
Created May 26, 2017 11:53
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 barneycarroll/7d56754d7569234c0366c4206d109a76 to your computer and use it in GitHub Desktop.
Save barneycarroll/7d56754d7569234c0366c4206d109a76 to your computer and use it in GitHub Desktop.
// EventComponents have a `handlers` hash which they can use to listen for DOM events on the document.
// These are bound and unbound safely to avoid lifecycle race condition exceptions.
export default class EventComponent extends React.Component {
componentDidMount() {
for(const key in this.events || {})
$(document).on(key, this.events[key])
}
componentWillUnmount() {
for(const key in this.events || {})
$(document).off(key, this.events[key])
}
}
import EventedComponent from './EventComponent'
export default class CustomComponent extends EventedComponent {
constuctor(){
this.events = {
OpeningTimes : (event, data) => {
this._doStuff(data)
}
}
}
componentDidMount() {
// Need to remember to do this though if we want to subclass componentDidMount - yuck :/
super.componentDidMount(...arguments);
// ...
}
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment