1. 程式人生 > >[React Intl] Format a Date Relative to the Current Date Using react-intl FormattedRelative

[React Intl] Format a Date Relative to the Current Date Using react-intl FormattedRelative

update control att tex use lan ada form -i

Given a date, we’ll use the react-intl FormattedRelative component to render a date in a human readable format, such as “2 days ago”, in various languages.

We‘ll also see how to set the frequency of this component‘s rendering to make our app update the text in real-time.

                <
FormattedRelative value={new Date(review.date)} style="numeric" />

技術分享

Since the FormattedRelative component udpate in real time, so you can also control the update interval:

                <FormattedRelative 
                  
value={new Date(review.date)} updateInterval={1000} style="numeric" />

Notice that it will cause extra unecessary component render.

[React Intl] Format a Date Relative to the Current Date Using react-intl FormattedRelative