Skip to content

appWithTranslation is causing the React state to not consist when doing client-side page navigation. #2259

Closed
@pgburrack

Description

@pgburrack

🐛 Bug Report

Wrapper _.app.js with the appWithTranslation HOC is causing the React state to not consist when doing client-side page navigation.

To Reproduce

I created this repo with a simple example that you can clone and try:
https://github.com/pgburrack/next-i18next-issue

In the example repo I created a DummyProvider with the initial state.

const [state, setState] = useState({
    a: null
  });

Steps to reproduce:

  1. start repo by running npm run dev + open http://localhost:3000/
  2. Open console developer tools. I would expect to see:
{a: null}
{a: null}
{a: 'foo'}
{a: 'foo'}

👆 this is the state that is in the DummyProvider.
3. Click on "Go to about page" (while keeping the console open)
image

You will see that the state in DummyProiver is initiated again.

{
  a: null
}

Note: if you comment out appWithTranslation and just export the App component.
you will see that the state is being kept on client-side navigation.

Expected behavior

When navigating to the /about page i would expect the DummayProvider to be

{a: 'foo'}

Your Environment

  • runtime version: Node, v18.15.0, testing on chrome
  • i18next version: "23.7.16"
  • "next-i18next": "15.0.0"
  • "react-i18next": "13.4.1"
  • "next": "12.2.4",
  • os: Mac
  • any other relevant information

P.S i found that if I pass options to appWithTranslation it will work as expected.

Example:

export default appWithTranslation(MyApp, {
  i18n: {
    defaultLocale: DefaultLocale,
    locales: SupportedLocales,
  })

The docs are not saying to do that as well, as we have that information in next-i18next.config.js so i am not sure why i would need to do that.

I suspect that the following line of code is causing the issue

https://github.com/i18next/next-i18next/blob/master/src/appWithTranslation.tsx#L120

    return i18n !== null ? (
      <I18nextProvider i18n={i18n}>
        <WrappedComponent {...props} />
      </I18nextProvider>
    ) : (
      <WrappedComponent key={locale} {...props} />
    )

I saw that when doing the navigation will disappear.

Could use help with this 🙏

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions