You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: simplify provider inheritance, add new APIs (#1387)
Also introduces `createIntl` and `RawIntlProvider`
fixes#1386fixes#1376
## Creating intl without using Provider
We've added a new API called `createIntl` that allows you to create an `IntlShape` object without using `Provider`. This allows you to format things outside of React lifecycle while reusing the same `intl` object. For example:
```tsx
import {createIntl, createIntlCache, RawIntlProvider} from 'react-intl'
// This is optional but highly recommended
// since it prevents memory leak
const cache = createIntlCache()
const intl = createIntl({
locale: 'fr-FR',
messages: {}
}, cache)
// Call imperatively
intl.formatNumber(20)
// Pass it to IntlProvider
<RawIntlProvider value={intl}>{foo}</RawIntlProvider>
```
This is especially beneficial in SSR where you can reuse the same `intl` object across requests.
Copy file name to clipboardExpand all lines: docs/API.md
+25Lines changed: 25 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,6 +12,7 @@ There are a few API layers that React Intl provides and is built on. When using
12
12
-[`useIntl` hook (currently available in 3.0.0 beta)](#useintl-hook-currently-available-in-300-beta)
13
13
-[`injectIntl` HOC](#injectintl-hoc)
14
14
-[`IntlShape`](#intlshape)
15
+
-[`createIntl`](#createintl)
15
16
-[Date Formatting APIs](#date-formatting-apis)
16
17
-[`formatDate`](#formatdate)
17
18
-[`formatTime`](#formattime)
@@ -86,6 +87,7 @@ React Intl provides:
86
87
87
88
1.[`useIntl` hook](#useintl-hook): to _hook_ the imperative formatting API into a React function component (with React version >= 16.8).
88
89
2.[`injectIntl` HOC](#injectintl-hoc): to _inject_ the imperative formatting API into a React class or function component via its `props`.
90
+
3.[`createIntl`](#createintl): to create `IntlShape` object outside of React lifecycle.
89
91
90
92
These should be used when your React component needs to format data to a string value where a React element is not suitable; e.g., a `title` or `aria` attribute, or for side-effect in `componentDidMount`.
91
93
@@ -198,6 +200,29 @@ The definition above shows what the `props.intl` object will look like that's in
198
200
-**`IntlConfig`:** The intl metadata passed as props into the parent `<IntlProvider>`.
199
201
-**`IntlFormatters`:** The imperative formatting API described below.
200
202
203
+
#### `createIntl`
204
+
205
+
This allows you to create an `IntlShape` object without using `Provider`. This allows you to format things outside of React lifecycle while reusing the same `intl` object. For example:
@@ -109,9 +109,25 @@ Assuming `navigator.language` is `"fr"`:
109
109
<div><span>mardi 5 avril 2016</span></div>
110
110
```
111
111
112
-
#### Multiple Intl Contexts
112
+
###`RawIntlProvider`
113
113
114
-
Nested `<IntlProvider>` components can be used to provide a different, or modified i18n context to a subtree of the app. In these cases, the nested `<IntlProvider>` will inherit from its nearest ancestor `<IntlProvider>`. A nested strategy can be employed to provide a subset of translations to a subtree. See: [Nested Example app](https://github.com/formatjs/react-intl/tree/master/examples/nested)
114
+
This is the underlying `React.Context.Provider` object that `IntlProvider` use. It can be used in conjunction with `createIntl`:
Copy file name to clipboardExpand all lines: docs/Upgrade-Guide.md
+28-1Lines changed: 28 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,6 +16,7 @@
16
16
-[Jest](#jest)
17
17
-[webpack babel-loader](#webpack-babel-loader)
18
18
-[Apostrophe Escape](#apostrophe-escape)
19
+
-[Creating intl without using Provider](#creating-intl-without-using-provider)
19
20
20
21
<!-- tocstop -->
21
22
@@ -32,7 +33,8 @@
32
33
33
34
-`FormattedRelative` has been renamed to `FormattedRelativeTime` and its API has changed significantly. See [FormattedRelativeTime](#formattedrelativetime) for more details.
34
35
-`formatRelative` has been renamed to `formatRelativeTime` and its API has changed significantly. See [FormattedRelativeTime](#formattedrelativetime) for more details.
35
-
- Escape character has been changed to apostrophe (`'`). See [Apostrophe Escape](#apostrophe-escape) for more details
36
+
- Escape character has been changed to apostrophe (`'`). See [Apostrophe Escape](#apostrophe-escape) for more details.
37
+
-`IntlProvider` no longer inherits from upstream `IntlProvider`.
36
38
37
39
## Use React 16.3 and upwards
38
40
@@ -350,3 +352,28 @@ Previously while we were using ICU message format syntax, our escape char was ba
350
352
```
351
353
352
354
We highly recommend reading the spec to learn more about how quote/escaping works [here](http://userguide.icu-project.org/formatparse/messages) under **Quoting/Escaping** section.
355
+
356
+
## Creating intl without using Provider
357
+
358
+
We've added a new API called `createIntl` that allows you to create an `IntlShape` object without using `Provider`. This allows you to format things outside of React lifecycle while reusing the same `intl` object. For example:
0 commit comments