Skip to content
Advertisement

Why does lodash throttle not work within the useWindowSize custom hooks?

I’m trying to use resize event with throttle. However, it doesn’t work. I tried to debug it as follow:

JavaScript

As seen from the code above, I’ve been trying to log out before I use the throttle func from lodash. It does print out, but the log inside the throttle itself doesn’t. Anyone knows why this maybe and how to fix this?

Advertisement

Answer

Your inline function recrated on every render. Just make sure the throttle function will be the same function on next render. You can use useCallback hook.

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