Skip to content

Instantly share code, notes, and snippets.

@danawoodman
Last active November 20, 2017 08:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danawoodman/eeec163d0a06b97d3a78 to your computer and use it in GitHub Desktop.
Save danawoodman/eeec163d0a06b97d3a78 to your computer and use it in GitHub Desktop.
set-interval-mixin.js
/*
SetIntervalMixin for React.
Usage:
var MyComponent = React.createClass({
getInitialState: function () {
return { value: 0 };
},
componentDidMount: function () {
// Increase value by 1 every second.
this.setInterval(function () {
this.setState({ value: this.state.value + 1 });
}.bind(this), 1000);
},
render: function () {
return <h1>Value: {this.state.value}</h1>;
}
});
*/
var SetIntervalMixin = {
componentWillMount: function () {
this.intervals = [];
},
componentWillUnmount: function () {
this.intervals.map(clearInterval);
},
setInterval: function () {
this.intervals.push(setInterval.apply(null, arguments));
}
};
module.exports = SetIntervalMixin;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment