Next-i18next: Cannot use props with components using translations

Created on 7 Dec 2020  Â·  2Comments  Â·  Source: isaachinman/next-i18next

Describe the bug

Using custom props next to the translation function t() throws an error with typescript.

Occurs in next-i18next version

7.0.1

Steps to reproduce

Create a component with custom props

function Nav({ propName, t }) {
   ...
}
export default withTranslation('nav')(Nav)

Use it in another component:

<Nav propName="test" />

Expected behaviour

No errors on compile.

Screenshots

If applicable, add screenshots or a GIF to help explain your problem.
image
image

Most helpful comment

Yes, I had the same problem.

In https://github.com/isaachinman/next-i18next/issues/647#issuecomment-596451214, one developer switched to Hooks instead, which are also used in Next.js' react-i18next sample app.

Therefore, one way is to do it like this:

export const nextI18Next = new NextI18Next({ … });
export const useTranslation = nextI18Next.useTranslation;

… and then …

import { useTranslation } from 'config/i18next';

const MyComponent = () => {
  const { t } = useTranslation();

  …

I have no idea if this is the best solution, but it feels like it is the solution

  • that needs the least code
  • and that fits well into this React Hooks mentality.

All 2 comments

Yes, I had the same problem.

In https://github.com/isaachinman/next-i18next/issues/647#issuecomment-596451214, one developer switched to Hooks instead, which are also used in Next.js' react-i18next sample app.

Therefore, one way is to do it like this:

export const nextI18Next = new NextI18Next({ … });
export const useTranslation = nextI18Next.useTranslation;

… and then …

import { useTranslation } from 'config/i18next';

const MyComponent = () => {
  const { t } = useTranslation();

  …

I have no idea if this is the best solution, but it feels like it is the solution

  • that needs the least code
  • and that fits well into this React Hooks mentality.

This is a problem that is not specific to next-i18next, but HOCs in TypeScript in general. Using hooks avoids this issue, but otherwise you'll need to have a look at how to type HOCs in TS. Good luck!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

denny7 picture denny7  Â·  6Comments

isaachinman picture isaachinman  Â·  7Comments

nataliaroque77 picture nataliaroque77  Â·  3Comments

ddereszewski picture ddereszewski  Â·  3Comments

revskill10 picture revskill10  Â·  5Comments