-
-
Save mutewinter/e2fcd172dc15b8f55ecd to your computer and use it in GitHub Desktop.
Update on Interval Higher Order Component
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 React, { Component } from 'react'; | |
import clock, { validIntervals } from '../utils/clock'; | |
const updateOnInterval = (interval = 1) => { | |
return BaseComponent => class extends Component { | |
static displayName = `updateOnInterval(${BaseComponent.displayName})`; | |
componentDidMount() { | |
this.subscription = clock.addListener( | |
`seconds.${interval}`, () => this.forceUpdate() | |
); | |
} | |
componentWillUnmount() { | |
this.subscription.remove(); | |
} | |
render() { | |
return <BaseComponent {...this.props}/>; | |
} | |
}; | |
}; | |
export default updateOnInterval; |
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 { EventEmitter } from 'fbemitter'; | |
const emitter = new EventEmitter(); | |
let total = 0; | |
setInterval(() => { | |
emitter.emit('seconds.1', ++total); | |
if (total % 5 === 0) { | |
emitter.emit(`seconds.5`, ++total); | |
} | |
}, 1000); | |
export default emitter; | |
export const validIntervals = [1, 5]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment