italia / design-react-kit

Il toolkit React conforme alle linee guida di design per i siti internet e i servizi digitali della PA.
https://italia.github.io/design-react-kit/
BSD 3-Clause "New" or "Revised" License
152 stars 80 forks source link

[BUG] Infinite re-renders when using useNavScroll #1057

Closed pantpad closed 2 months ago

pantpad commented 2 months ago

Describe the bug Using the useNavScroll hook and its register function triggers an infinite re-rendering of the component.

To Reproduce Steps to reproduce the behavior:

  1. Install the dependencies as described in the italia/design-react-kit
  2. Go to (https://italia.github.io/design-react-kit/?path=/docs/documentazione-menu-di-navigazione-navscroll--documentazione)
  3. Copy and paste first example inside your own component.
  4. Add console.log to check amount of re-renders.
  5. Run the app and check the waterfall of logs that keeps appearing as soon as the component loads (i'm not scrolling down or up)

Expected behavior The useNavScroll seems to work as intended, it simply creates an infinite re-render of the page.

Screenshots Screenshot 2023-06-12 165102

Virtute90 commented 2 months ago

A new render is performed every time the page is "scrolled", this is how the component works.

At each renderer the references of where you have arrived are updated and the navigation menu is updated.

pantpad commented 2 months ago

A new render is performed every time the page is "scrolled", this is how the component works.

At each renderer the references of where you have arrived are updated and the navigation menu is updated.

The problem is that even if I don't scroll or interact with the page in any way, re-renderings keep happening. In a couple of seconds I get almost 1000 re-renders.

Is this supposed to happen as well?

Nick87 commented 2 months ago

I was able to reproduce and hopefully fix the issue that @pantpad reported: please take a look at #1061.

astagi commented 2 months ago

@Nick87 thank you so much! @pantpad a new version (5.0.4) is now available on NPM with #1061 included! let us know if everything runs fine now!