forked from TylerLH/react-native-timeago
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimeAgo.js
47 lines (40 loc) · 857 Bytes
/
TimeAgo.js
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
// @flow
import React, { Component } from "react";
import { View, Text } from "react-native";
import moment from "moment";
export default class TimeAgo extends Component {
props: {
time: string,
interval?: number,
hideAgo?: boolean
};
state: { timer: null | number } = { timer: null };
static defaultProps = {
hideAgo: false,
interval: 60000
};
componentDidMount() {
this.createTimer();
}
createTimer = () => {
this.setState({
timer: setInterval(() => {
this.update();
}, this.props.interval)
});
};
componentWillUnmount() {
clearInterval(this.state.timer);
}
update = () => {
this.forceUpdate();
};
render() {
const { time, hideAgo } = this.props;
return (
<Text {...this.props}>
{moment(time).fromNow(hideAgo)}
</Text>
);
}
}