Created
March 15, 2020 02:09
-
-
Save jaccon/d5360f6268fa399022756d9eec791f3d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// @flow | |
import React, { Component } from "react"; | |
import { View, Text } from "react-native"; | |
import moment from "moment"; | |
import 'moment/locale/pt-br'; | |
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; | |
const postDate = moment(time).locale('pt-br').fromNow(hideAgo); | |
return ( | |
<Text {...this.props}> | |
{postDate} | |
</Text> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Implementando o MomentJS com local BRL para traduções de datas no React Native