Created
May 26, 2017 11:53
-
-
Save barneycarroll/7d56754d7569234c0366c4206d109a76 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
// 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]) | |
} | |
} |
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
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