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.

useTranslation is the cause of the rerender component

See original GitHub issue

šŸ› Bug Report

I got rerendering issue while use const { t } = useTranslation() of gatsby-plugin-react-i18next. It is the same with t API get from useTranslation of react-i18next and useI18next of this library.

I’ve confirmed in react-i18next, seems like there is no issue with it.

https://github.com/i18next/react-i18next/issues/1291#issuecomment-813844756 https://github.com/welldone-software/why-did-you-render/issues/187

Could you please give me the idea to prevent a re-rendering issue every time use t of useTranslation? Thanks šŸ™‡šŸ»

To Reproduce

Repo: https://github.com/zcmgyu/react-i18next-rerender-issue

Screen Shot 2021-04-06 at 12 33 17 Screen Shot 2021-04-06 at 12 33 33

There is no rerender issue if using react-i18next with create-react-app project.

https://codesandbox.io/s/why-did-you-render-sandbox-forked-891xf?file=/src/index.js

Expected behavior

Could useTranslation feature without re-render because of the same name of fixedT

Your Environment

  • runtime version: Node v15.8.0, browser Chrome
  • i18next version: ^20.1.0
  • react-i18next: ^11.8.12
  • gatsby-plugin-react-i18next: ^1.1.1
  • os: MacOS Big Sur

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
andrewkwwcommented, Oct 20, 2021

Looks like I misunderstood. I did my <Trans> test in plain gatsby. This only happens in Storybook? Maybe there’s something weird going on there. Perhaps the t() or i18n is being cached somewhere, making the element not getting updated. But I guess that’s off topic here.

Though it does look like this repo is becoming dormant. I guess we’d have to build our own npm package for the time being.

2reactions
andrewkwwcommented, Oct 15, 2021

I’m running into this issue too. I think I managed to track down why it’s rerendering.

During the first render (first call to wrapPageElement), i18n.init() actually doesn’t fully initialize itself because it doesn’t have the translation resources yet. This can be seen here:

https://github.com/i18next/i18next/blob/c2eccedbc13295e94b98f14ff85f2bd377d774e0/src/i18next.js#L193-L197

And so the rest of the initialization is deferred and gets done later asynchronously.

So for the rest of the first render, i18n remains half-initialized while the rest of the page gets rendered. And all the calls to useTranslation() notice this, and setup some callbacks to wait for i18n to fully initialize. So once the resources are loaded, and i18n actually completely initialized, all the calls to useTranslation() get updated, which is why all the rerendering is happening.

Knowing this, the fix is actually really simple.

Currently, the plugin loads the resources right after i18n.init(): https://github.com/microapps/gatsby-plugin-react-i18next/blob/c6dabc14200a68003f7b02f934fa47bacabf539f/src/plugin/wrapPageElement.tsx#L119-L122

So all we need to do is build the resources first, and pass it into i18n.init(), so that it’ll initialize immediately.

In my scenario, the profiling result went from this: image to this: image

Read more comments on GitHub >

github_iconTop Results From Across the Web

useTranslation is the cause of the rerender component #1291
I got rerendering issue while use const { t } = useTranslation() of react-i18next. In my research, the inside of useTranslation hook usedĀ ......
Read more >
useTranslation (hook) - react-i18next documentation
It gets the t function and i18n instance inside your functional component. import React from 'react';. import { useTranslation } from 'react-i18next';.
Read more >
next.js - Excessive use of useTranslation() hook - Stack Overflow
I'm wondering if using const { t } = useTranslation('common') everywhere for each react component would have a negative impact onĀ ...
Read more >
A Guide to React Localization with i18next | Phrase
Accessing the i18next instance in React components. If we're using the react-i18next useTranslation() hook, we can just destructure the i18n object out of...
Read more >
React - Transifex Developer Hub
You can easily localize React components using the @transifex/react library ... Makes the current component re-render when a language change is detected and...
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