Skip to content

Instantly share code, notes, and snippets.

@leanazulyoro
Created August 22, 2019 12:30
Show Gist options
  • Save leanazulyoro/2f62b16da9dd9b3ede4bb307e87a714f to your computer and use it in GitHub Desktop.
Save leanazulyoro/2f62b16da9dd9b3ede4bb307e87a714f to your computer and use it in GitHub Desktop.
For medium article: react code sharing history. Mixins
var SetIntervalMixin = {
componentWillMount: function() {
this.intervals = [];
},
setInterval: function() {
this.intervals.push(setInterval.apply(null, arguments));
},
componentWillUnmount: function() {
this.intervals.map(clearInterval);
},
};
var TimeAgo = React.createClass({
mixins: [SetIntervalMixin],
render: function () {
return (
<small className="time-ago">{this.state.time}</small>
);
},
componentDidMount: function () {
this.setInterval(this.updateTime, 1000);
},
getInitialState: function () {
return { time: moment(this.props.children).fromNow() };
},
updateTime: function () {
this.setState({ time: moment(this.props.children).fromNow() });
},
});
var TimeAgo = React.createClass({
componentWillMount: function() {
this.intervals = [];
},
setInterval: function() {
this.intervals.push(setInterval.apply(null, arguments));
},
componentWillUnmount: function() {
this.intervals.map(clearInterval);
},
render: function () {
return (
<small className="time-ago">{this.state.time}</small>
);
},
componentDidMount: function () {
this.setInterval(this.updateTime, 1000);
},
getInitialState: function () {
return { time: moment(this.props.children).fromNow() };
},
updateTime: function () {
this.setState({ time: moment(this.props.children).fromNow() });
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment