React/Typescript Code blog
Today I will be describing how to build a simple,
performant React hook for tracking the window size
as the user resizes a window. This approach uses
requestAnimationFrame
which places less
burden on your UI thread because it is only
called when the UI needs to be redrawn, instead of
on every resize event.
Tracking window size with React hooks
Here is an example using React hooks and requestAnimationFrame
. This also uses pure js without any libraries like lodash,
Here is a gist showing it in use: Img.tsx with useResize. Alternatively, you can see it in my repo here for more context.
Some resources on why you should do this instead of debouncing your function:
- a relevant github pr with good explanation
- medium but behind a meme wall
- an in-depth stackoverflow answer
Thank you for coming to my Ted Talk.