Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(react-intl): fix FormattedRelativeTime update after unmount, fix #…
- Loading branch information
Showing
4 changed files
with
106 additions
and
87 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import React, {useState} from 'react' | ||
import {IntlProvider, FormattedRelativeTime, useIntl} from 'react-intl' | ||
|
||
const getRelativeTime = (date: Date) => | ||
(date.getTime() - new Date().getTime()) / 1000 | ||
|
||
const PostDate: React.FC<{date: Date}> = ({date}) => { | ||
const intl = useIntl() | ||
return ( | ||
<span title={intl.formatDate(date)}> | ||
<FormattedRelativeTime | ||
value={getRelativeTime(date)} | ||
updateIntervalInSeconds={1} | ||
/> | ||
</span> | ||
) | ||
} | ||
|
||
export const App: React.FC<{}> = () => { | ||
const [hide, setHide] = useState(false) | ||
return ( | ||
<IntlProvider locale={navigator.language}> | ||
<div> | ||
<h1>Hello world</h1> | ||
{!hide && ( | ||
<p> | ||
<PostDate date={new Date()} /> | ||
</p> | ||
)} | ||
|
||
<button onClick={() => setHide(current => !current)}> | ||
Toggle FormattedRelativeTime | ||
</button> | ||
</div> | ||
</IntlProvider> | ||
) | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,44 @@ | ||
import 'core-js/stable'; | ||
import '@formatjs/intl-pluralrules/polyfill'; | ||
import * as React from 'react'; | ||
import * as ReactDOM from 'react-dom'; | ||
import Timezone from './TimeZone'; | ||
import Messages from './Messages'; | ||
import Hooks from './Hooks'; | ||
import {bootstrapApplication} from './advanced/Advanced'; | ||
import Injected from './Injected'; | ||
import HandleChange from './HandleChange'; | ||
import StaticTypeSafetyAndCodeSplitting from './StaticTypeSafetyAndCodeSplitting/StaticTypeSafetyAndCodeSplitting'; | ||
|
||
import 'core-js/stable' | ||
import '@formatjs/intl-pluralrules/polyfill' | ||
import * as React from 'react' | ||
import * as ReactDOM from 'react-dom' | ||
import Timezone from './TimeZone' | ||
import Messages from './Messages' | ||
import Hooks from './Hooks' | ||
import {bootstrapApplication} from './advanced/Advanced' | ||
import Injected from './Injected' | ||
import HandleChange from './HandleChange' | ||
import StaticTypeSafetyAndCodeSplitting from './StaticTypeSafetyAndCodeSplitting/StaticTypeSafetyAndCodeSplitting' | ||
import {App as Bug2727} from './Bug2727' | ||
//polyfills | ||
import '@formatjs/intl-getcanonicallocales/polyfill'; | ||
import '@formatjs/intl-locale/polyfill'; | ||
import '@formatjs/intl-getcanonicallocales/polyfill' | ||
import '@formatjs/intl-locale/polyfill' | ||
|
||
import '@formatjs/intl-pluralrules/polyfill'; | ||
import '@formatjs/intl-pluralrules/locale-data/en'; // locale-data for en | ||
import '@formatjs/intl-pluralrules/polyfill' | ||
import '@formatjs/intl-pluralrules/locale-data/en' // locale-data for en | ||
|
||
import '@formatjs/intl-numberformat/polyfill'; | ||
import '@formatjs/intl-numberformat/locale-data/en'; // locale-data for en | ||
import '@formatjs/intl-numberformat/polyfill' | ||
import '@formatjs/intl-numberformat/locale-data/en' // locale-data for en | ||
|
||
import '@formatjs/intl-datetimeformat/polyfill'; | ||
import '@formatjs/intl-datetimeformat/locale-data/en'; // locale-data for en | ||
import '@formatjs/intl-datetimeformat/add-all-tz'; // Add ALL tz data | ||
import '@formatjs/intl-datetimeformat/polyfill' | ||
import '@formatjs/intl-datetimeformat/locale-data/en' // locale-data for en | ||
import '@formatjs/intl-datetimeformat/add-all-tz' // Add ALL tz data | ||
|
||
ReactDOM.render(<Timezone />, document.getElementById('timezone')); | ||
ReactDOM.render(<Timezone />, document.getElementById('timezone')) | ||
|
||
ReactDOM.render(<Messages />, document.getElementById('messages')); | ||
ReactDOM.render(<Hooks />, document.getElementById('hooks')); | ||
ReactDOM.render(<Messages />, document.getElementById('messages')) | ||
ReactDOM.render(<Hooks />, document.getElementById('hooks')) | ||
|
||
ReactDOM.render(<Injected />, document.getElementById('injected')); | ||
ReactDOM.render(<HandleChange />, document.getElementById('handlechange')); | ||
ReactDOM.render(<Injected />, document.getElementById('injected')) | ||
ReactDOM.render(<HandleChange />, document.getElementById('handlechange')) | ||
|
||
ReactDOM.render( | ||
<StaticTypeSafetyAndCodeSplitting />, | ||
document.getElementById('static-type-safety-and-code-splitting') | ||
); | ||
) | ||
|
||
ReactDOM.render(<Bug2727 />, document.getElementById('bug2727')) | ||
|
||
bootstrapApplication('en').then(app => { | ||
ReactDOM.render(app, document.getElementById('advanced')); | ||
}); | ||
ReactDOM.render(app, document.getElementById('advanced')) | ||
}) |
This file contains 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