Skip to content
Advertisement

React: how to avoid re-rendering a component with a hook that returns always the same value but changes its inner state

imagine to have this hook that changes his value every second and returns true if the random value is a multiple of 5 otherwise false. what can I do to stop re-rendering every second?

PS: I tried useMemo, and useRef to return back always the same object but it’s still re-rendering.

please help

JavaScript

Advertisement

Answer

I could fix that issue using this library react-hooks-in-callback isolating the hook from the component.

check this sandbox example

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement