Skip to content

Instantly share code, notes, and snippets.

@mutewinter
Last active February 4, 2016 15:56
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 mutewinter/e2fcd172dc15b8f55ecd to your computer and use it in GitHub Desktop.
Save mutewinter/e2fcd172dc15b8f55ecd to your computer and use it in GitHub Desktop.
Update on Interval Higher Order Component
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;
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