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.

i18nLoadedAt prop sent even if cached - triggering unnecessary re-renders

See original GitHub issue

Initially (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:

  1. loading the page (primes the i18n cache, specifically the common namespace)
  2. triggering the authentication dialog (same page)
  3. clearing the log
  4. 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:

  1. the i18n is cached and did not change
  2. my component has not changed and does not need to be re-rendered

Questions/solutions

  1. What is the intended purpose of i18nLoadedAt?
  2. Why is the i18nLoadedAt passed when the namespace in question is cached?
  3. 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:closed
  • Created 7 years ago
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
rosskevincommented, Oct 19, 2016
  • bindI18n: false had no effect
  • bindStore: 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 like myTranslate(translate(pure(relayContainer(Foo)))), so any recommended global config or pattern is appreciated. Thank you

0reactions
jamuhlcommented, Mar 17, 2018

@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

Read more comments on GitHub >

github_iconTop 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 >

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