Last active
November 20, 2017 08:09
-
-
Save danawoodman/eeec163d0a06b97d3a78 to your computer and use it in GitHub Desktop.
set-interval-mixin.js
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
/* | |
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