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.

Many probably unnecessary renders

See original GitHub issue

Describe the bug After some time working in dev, my app with next-i18next started to get sluggish on page reloads and language changes. I installed ‘why-did-you-update’ to investigate and found loads of seemingly unnecessary rerenders of the same components happening. The additional renders pile up with reloads and language changes and don’t happen again afterwards.

It seems the number of rerenders depend on the number of components using withNamespaces. While it is understandable that a change of language causes a rerender of every connected component, it looks like every time the HOC triggers a render in one connected component, it causes all connected components to render.

Word of warning: It might be that why-did-you-update is not suited to debug server-side rendering.

To Reproduce Steps to reproduce the behaviour:

  1. Clone or download this repository.
  2. Install why-did-you-update for the example:
cd example
yarn add why-did-you-update
  1. Init why-did-you-update in _app.js in /example/pages:
import React from 'react'
import App, {Container} from 'next/app'

if (process.env.NODE_ENV !== 'production') {
  const { whyDidYouUpdate } = require('why-did-you-update');
  whyDidYouUpdate(React);
}
...
  1. Run the example: yarn run run-example

Expected behaviour The console in the browser should print (ideally only once)

LoadNamespace(Title)
LoadNamespace(Link)
LoadNamespace(Footer)

Actual behaviour The console in the browser prints the three lines six times. When clicking Change locale it outputs the three lines ten times.

OS (please complete the following information):

  • Device: MacBook Pro (15-inch, 2017)
  • Browser: Chrome Version 70.0.3538.110

Additional Information

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
jamuhlcommented, May 13, 2019

@oMatej the t function is equal until languageChange or any other bound event to trigger a rerender: https://github.com/i18next/react-i18next/blob/v9.x.x/src/NamespacesConsumer.js#L92

1reaction
isaachinmancommented, Apr 18, 2019

Also as a general note to everyone - don’t optimise prematurely.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How I eliminate ALL unnecessary Rerenders in React - Medium
1. Ensure I don't encode any infinite loops. The most crucial outcome of unnecessary re-renders is when you include infinite loops in your...
Read more >
Avoiding Unnecessary Renders in React - KIRUPA
Optimize the performance of your app by learning how to reduce the number of unnecessary render method calls!
Read more >
How to debug unnecessary rerenders in React | Bryce's Blog
Thankfully React gives developers several tools to help them find the source of unnecessary rerenders. In this post I'll discuss three of ...
Read more >
How to identify and resolve wasted renders in React
Configuring each component to only render and differentiate when it is necessary. This will allow us to reclaim those wasted CPU cycles. First, ......
Read more >
How can I reduce unnecessary re-renders in react without ...
The problem I am encountering is slow UI due to multiple re-renders. I display the modals based on state variables declared in their ......
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