forked from TylerLH/react-native-timeago
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeAgo.js
More file actions
53 lines (44 loc) · 1.13 KB
/
TimeAgo.js
File metadata and controls
53 lines (44 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
var React = require('react')
var ReactNative = require('react-native');
var moment = require('moment');
var TimerMixin = require('react-timer-mixin');
var createReactClass = require('create-react-class');
var { Text } = ReactNative;
import PropTypes from 'prop-types';
var TimeAgo = createReactClass({
mixins: [TimerMixin],
propTypes: {
time: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
PropTypes.array,
PropTypes.instanceOf(Date)
]).isRequired,
interval: PropTypes.number,
hideAgo: PropTypes.bool
},
getDefaultProps() {
return {
hideAgo: false,
interval: 60000
}
},
componentDidMount() {
var {interval} = this.props;
this.setInterval(this.update, interval);
},
componentWillUnmount() {
this.clearInterval(this.update);
},
// We're using this method because of a weird bug
// where autobinding doesn't seem to work w/ straight this.forceUpdate
update() {
this.forceUpdate();
},
render() {
return (
<Text {...this.props}>{moment(this.props.time).fromNow(this.props.hideAgo)}</Text>
);
}
});
module.exports = TimeAgo;