forked from TylerLH/react-native-timeago
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimeAgo.js
50 lines (43 loc) · 949 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
48
49
50
// @flow
import React, { Component } from "react";
import { Text } from "react-native";
import dayjs from 'dayjs'
import relativeTime from 'dayjs/plugin/relativeTime'
dayjs.extend(relativeTime)
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: setTimeout(() => {
this.update();
}, this.props.interval)
});
};
componentWillUnmount() {
clearTimeout(this.state.timer);
}
update = () => {
this.forceUpdate();
this.createTimer();
};
render() {
const { time, hideAgo } = this.props;
return (
<Text {...this.props}>
{dayjs(time).fromNow(hideAgo)}
</Text>
);
}
}