Skip to content

Instantly share code, notes, and snippets.

@mutewinter
Last active Feb 4, 2016
Embed
What would you like to do?
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