Usage of useTranslation hook inside components cause rerender
See original GitHub issueDescribe 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:
- Created 4 years ago
- Reactions:5
- Comments:30 (8 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
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 😬