Skip to content

Instantly share code, notes, and snippets.

@jaccon
Created March 15, 2020 02:09
Show Gist options
  • Save jaccon/d5360f6268fa399022756d9eec791f3d to your computer and use it in GitHub Desktop.
Save jaccon/d5360f6268fa399022756d9eec791f3d to your computer and use it in GitHub Desktop.
// @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>
);
}
}
@jaccon
Copy link
Author

jaccon commented Mar 15, 2020

Implementando o MomentJS com local BRL para traduções de datas no React Native

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment