question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Usage of useTranslation hook inside components cause rerender

See original GitHub issue

Describe the bug

When I use useTranslation hook inside function component it causes multiple rerenders. Can’t reproduce it with create-react-app and react-i18next. So it should be landed here. If I use react profiler I see that it takes 1102 render, but with create-react-app and react-i18next it takes only 2. Am I missing something or this is a bug?

Occurs in next-i18next version

4.2.1

Steps to reproduce

# i18n.js

const NextI18Next = require('next-i18next').default

module.exports = new NextI18Next({
    defaultLanguage: 'fi',
    otherLanguages: ['en', 'ru'],
    localeSubpaths: {
        en: 'en',
        ru: 'ru',
    },
})

# _app.js

import React from 'react';
import './App.css';
import Test from './components/test';
import i18n from './i18n';

const Locales = () => (
  <>
    {['fi', 'en', 'ru'].map(locale => (
      <button type="button" key={locale} onClick={() => { i18n.changeLanguage(locale); }}>
        {locale}
      </button>
    ))}
  </>
);

function App() {
  let tests = [];

  for (let index = 0; index < 100; index++) {
    let testsinner = [];
    for (let index2 = 0; index2 < 10; index2++) {
      testsinner.push(<Test key={`${index}_${index2}`} />);
    }
    tests.push(<Test key={index}>{testsinner}</Test>);
  }

  return (
    <>
      <Locales />
      <div>
        {tests}
      </div>
    </>
  );

}

export default App;
# components/test.jsx

import i18n from '../i18n';

let count = 0;

const Test = ({ children }) => {
    count++;

    const [t, { language }] = i18n.useTranslation();

    return (
        <>
            <p>{t('catalog')} + {count} + {language}</p>
            <div>{children}</div>
        </>
    )
}

export default Test;

Expected behaviour

Shouldn’t be rendered so much times

OS (please complete the following information)

  • OS: macOS Catalina
  • Browser: Chrome 80.0.3987.132

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:30 (8 by maintainers)

github_iconTop GitHub Comments

13reactions
k4mr4ncommented, Nov 6, 2020

I have the same issue and whydidyourender library point it out very well. it seems like the “t” function exported from “useTranslation” hook has not been memoized.

Screen Shot 2020-11-06 at 9 28 05 AM
4reactions
tquirogacommented, Dec 29, 2020

Same issue pointed by whydidyourender, when you use the hook across your app and realize most re-render are caused by the useTranslation hook, ouch 😬

Read more comments on GitHub >

github_iconTop Results From Across the Web

Usage of useTranslation hook inside components cause ...
When I use useTranslation hook inside function component it causes multiple rerenders. Can't reproduce it with create-react-app and ...
Read more >
useTranslation (hook)
Use the useTranslation hook inside your functional components to access the translation function or i18n instance. useTranslation params ...
Read more >
next.js - Excessive use of useTranslation() hook
I'm wondering if using const { t } = useTranslation('common') everywhere for each react component would have a negative impact on performance.
Read more >
Rendered more hooks than during the previous ...
The error "Rendered more hooks than during the previous render" occurs when we conditionally call a hook or return early before all hooks...
Read more >
How to escape React Hooks Hell
Don't useMemo entire inner components. Once people understand how useMemo can be used to improve performance, it becomes easy to be used poorly....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found