i18nLoadedAt prop sent even if cached - triggering unnecessary re-renders
See original GitHub issueInitially (without caching) I was seeing 14 re-renders on a simple page which I thought was way beyond reasonable, thinking we have fundamentally coded something incorrectly.
I found i18nLoadedAt
prop to be sent even if the locale is cached. I have created a Performance
class that I run shouldComponentUpdate
through and it logs any diff in development.
I have primed the following scenario by:
- loading the page (primes the i18n cache, specifically the
common
namespace) - triggering the authentication dialog (same page)
- clearing the log
- triggering the authentication dialog (same as 2 above)
Here is the sequence:
# trigger the dialog
[Performance] [AppFrame] is dirty: state (key showAuthentication is diff)
[i18n] translator: missingKey - en common %s - $t(domain) %s - $t(domain)
[Performance] [AppFrame] is dirty: props ([i18nLoadedAt] keys are diff)
[18n] translator: missingKey - en common %s - $t(domain) %s - $t(domain)
So, the prop i18nLoadedAt
is marking my component as dirty, even though:
- the i18n is cached and did not change
- my component has not changed and does not need to be re-rendered
Questions/solutions
- What is the intended purpose of
i18nLoadedAt
? - Why is the
i18nLoadedAt
passed when the namespace in question is cached? - As a hack (which I don’t like), I could ignore
i18nLoadedAt
, but this doesn’t seem like the right thing to do, and depending on 1 above, it may harm i18n non-cached locale pages and dynamic updating.
Thoughts?
Issue Analytics
- State:
- Created 7 years ago
- Comments:14 (7 by maintainers)
Top Results From Across the Web
How to debug unnecessary rerenders in React | Bryce's Blog
Hooks changing (ie useState 's setState methode being called); props changing (it will list the exact props!) a component's parent rerendering.
Read more >Optimizing React performance by preventing unnecessary re ...
This article explains how to update components only when necessary, and how to avoid common causes of unintentional re-renders.
Read more >How I eliminate ALL unnecessary Rerenders in React - Medium
Use dynamic programming. We all know that React re-renders a component every time its props or state changes. But this means that even...
Read more >5 Ways to Avoid React Component Re-Renderings
1. Memoization using useMemo() and UseCallback() Hooks ... Memoization enables your code to re-render components only if there's a change in the props....
Read more >Understanding re-rendering and memoization in React
Generally, a re-render is caused by a component's props or state changing. When a component re-renders all of its children components will ...
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 FreeTop 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
Top GitHub Comments
bindI18n: false
had no effectbindStore: false
worked!(options above tried with
wait: true
)So minimally with our cached setup, it seems we always want
{wait: true, bindStore: false}
on every translate call.Is there a way to default these globally so I don’t need to add these options in all our components? I see in the source that they are defaulted in the
#translate
options if not specified. I could wrap it in a function, but it seems like we are adding up a ton of HOCs on an actual widget. If I did that, my widget would actually look likemyTranslate(translate(pure(relayContainer(Foo))))
, so any recommended global config or pattern is appreciated. Thank you@zarela yes beside that you now can set that globally too: https://react.i18next.com/components/i18next-instance.html
or by using the context API (our own): https://github.com/i18next/react-i18next/blob/master/src/context.js#L13