Skip to content

useTranslation is the cause of the rerender component #1636

@developer82

Description

@developer82

🐛 Bug Report

When using the useTranslation hook the component is re-rendered even if the translation is not used and when there is no change to the language.
I have found a similar reported issue here: #1291 but it was closed with the explanation that there is not issue.
To rule out a situation that it is something in my app that I'm missing, I've created a new react app without anything in it and reproduced the issue.

To Reproduce

  1. Create a new react app using npx create-react-app my-app
  2. Add the translation as described here: https://react.i18next.com/latest/using-with-hooks
  3. Add a console.log("App component loading"); in App component inside the App function.
  4. Once const { t, i18n } = useTranslation(); is added "App component loading" is printed twice in the console. When removed it is ok.
import logo from './logo.svg';
import './App.css';
import { useTranslation } from 'react-i18next';

function App() {
  const { t, i18n } = useTranslation();

  console.log("App component loading");

  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>
          Edit <code>src/App.js</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          Learn React
        </a>
      </header>
    </div>
  );
}

export default App;

Expected behavior

Component should not re-render by the useTranslation hook.

Your Environment

  • runtime version: node v18.12.1
  • i18next version: 22.4.15
  • os: Windows
  • i18next-browser-languagedetector: 7.0.1
  • react-i18next: 12.2.2

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