Skip to content

Commit

Permalink
feat: remove withI18n (#1503)
Browse files Browse the repository at this point in the history
  • Loading branch information
vonovak authored Mar 13, 2023
1 parent 3d07510 commit 580e702
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 156 deletions.
13 changes: 8 additions & 5 deletions packages/react/src/I18nProvider.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from "react"
import { act, render } from "@testing-library/react"

import { withI18n, I18nProvider } from "./I18nProvider"
import { I18nProvider, useLingui } from "./I18nProvider"
import { setupI18n } from "@lingui/core"
// eslint-disable-next-line import/no-extraneous-dependencies
import { mockConsole } from "@lingui/jest-mocks"
Expand All @@ -15,16 +15,19 @@ describe("I18nProvider", () => {
},
})

const WithoutHoc = (props) => {
const WithoutLingui = (props) => {
return <div {...props}>{props?.i18n?.locale}</div>
}

const WithHoc = withI18n()(WithoutHoc)
const WithLingui = (props) => {
const { i18n } = useLingui()
return <WithoutLingui i18n={i18n} {...props} />
}

const { getByTestId } = render(
<I18nProvider i18n={i18n}>
<WithoutHoc data-testid="not-composed" />
<WithHoc data-testid="composed" />
<WithoutLingui data-testid="not-composed" />
<WithLingui data-testid="composed" />
</I18nProvider>
)

Expand Down
29 changes: 0 additions & 29 deletions packages/react/src/I18nProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ export type I18nContext = {
defaultComponent?: ComponentType<TransRenderProps>
}

export type withI18nProps = {
i18n: I18n
}

export type I18nProviderProps = I18nContext & {
forceRenderOnLocaleChange?: boolean
children?: React.ReactNode
Expand All @@ -30,31 +26,6 @@ export function useLingui(): I18nContext {
return context
}

export function withI18n(
o?: object
): <P extends withI18nProps>(
Component: ComponentType<P>
) => React.ComponentType<Omit<P, "i18n">> {
return <P extends withI18nProps>(
WrappedComponent: ComponentType<P>
): ComponentType<P> => {
return (props) => {
if (process.env.NODE_ENV !== "production") {
if (typeof o === "function" || React.isValidElement(o)) {
throw new Error(
"withI18n([options]) takes options as a first argument, " +
"but received React component itself. Without options, the Component " +
"should be wrapped as withI18n()(Component), not withI18n(Component)."
)
}
}

const { i18n } = useLingui()
return <WrappedComponent {...props} i18n={i18n} />
}
}
}

export const I18nProvider: FunctionComponent<I18nProviderProps> = ({
i18n,
defaultComponent,
Expand Down
8 changes: 2 additions & 6 deletions packages/react/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
export { I18nProvider, useLingui, withI18n } from "./I18nProvider"
export { I18nProvider, useLingui } from "./I18nProvider"

export type {
I18nProviderProps,
I18nContext,
withI18nProps,
} from "./I18nProvider"
export type { I18nProviderProps, I18nContext } from "./I18nProvider"

export { Trans } from "./Trans"
export type { TransProps, TransRenderProps } from "./Trans"
95 changes: 0 additions & 95 deletions website/docs/guides/optimized-components.md

This file was deleted.

2 changes: 1 addition & 1 deletion website/docs/guides/testing.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Testing

Components using [`Trans`](/docs/ref/react.md#trans), [`withI18n`](/docs/ref/react.md#withi18n) or [`useLingui`](/docs/ref/react.md#uselingui) require access to the context of [`I18nProvider`](/docs/ref/react.md#i18nprovider). How you can wrap your component with the I18nProvider depends on the test library you use.
Components using [`Trans`](/docs/ref/react.md#trans) or [`useLingui`](/docs/ref/react.md#uselingui) require access to the context of [`I18nProvider`](/docs/ref/react.md#i18nprovider). How you can wrap your component with the I18nProvider depends on the test library you use.

Here is a working example with [react-testing-library](https://testing-library.com/docs/react-testing-library/intro/), using the [wrapper-property](https://testing-library.com/docs/react-testing-library/api#wrapper):

Expand Down
2 changes: 1 addition & 1 deletion website/docs/misc/react-intl.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ Custom IDs are supported as well:
```
:::note
To inject `i18n` object into props, you need to use HOC [`withI18n`](/docs/ref/react.md#withi18n). It's very similar to `injectIntl` from [react-intl](https://github.com/yahoo/react-intl). Alternatively, you can also use `I18n` render prop component.
To inject `i18n` object into props, you need to use [`useLingui`](/docs/ref/react.md#useLingui) hook. It's very similar to `useIntl` from [react-intl](https://formatjs.io/docs/react-intl/api/#useintl-hook).
:::
## External message catalog
Expand Down
15 changes: 1 addition & 14 deletions website/docs/ref/react.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ import { Trans } from '@lingui/react';

## Providers

Message catalogs and the active locale are passed to the context in [`I18nProvider`](#i18nprovider). Use [`useLingui`](#uselingui) hook or [`withI18n`](#withi18n) high-order component to access Lingui context.
Message catalogs and the active locale are passed to the context in [`I18nProvider`](#i18nprovider). Use [`useLingui`](#uselingui) hook to access Lingui context.

### I18nProvider

Expand Down Expand Up @@ -195,16 +195,3 @@ const CurrentLocale = () => {
return <span>Current locale: {i18n.locale}</span>
}
```

### withI18n

`withI18n` is a higher-order component which injects `i18n` object to wrapped component. `i18n` object is needed when you have to access the i18n data:

``` jsx
import React from "react"
import { withI18n } from "@lingui/react"

const CurrentLocale = withI18n()(({ i18n }) => (
<span>Current locale: {i18n.locale}</span>
))
```
6 changes: 6 additions & 0 deletions website/docs/releases/migration-4.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,9 @@ You can safely remove `i18n.loadLocaleData` calls because Lingui v4 uses `Intl.P
```

Don't forget to delete `make-plural` from your `package.json`.

### withI18n HOC was removed

`withI18n` HOC was removed in favor of `useLingui` hook. Please use `useLingui` instead.

If you need to use `withI18n` in your project, you can copy [the original implementation](https://github.com/lingui/js-lingui/blob/31dcab5a9a8f88bfa8b3a2c7cd12aa2d908a1d80/packages/react/src/I18nProvider.tsx#L33).
5 changes: 0 additions & 5 deletions website/sidebars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,6 @@ const sidebar = [
label: 'Dynamic loading',
id: 'guides/dynamic-loading-catalogs',
},
{
type: 'doc',
label: 'Optimized components',
id: 'guides/optimized-components',
},
{
type: 'doc',
label: 'Pluralization',
Expand Down

0 comments on commit 580e702

Please sign in to comment.