Explore the step by step implementation of useWindowSize hook in react which can help to know the window size to make responsive apps.
Anuj Sharma
Last Updated Nov 21, 2025
In React applications, creating responsive designs is important to provide a seamless user experience across various devices. In responsive design implementation, one common requirement is to know the size of the browser window to adapt the UI accordingly.
In this blog post, we'll delve into implementing a custom useWindowSize hook in React to dynamically track the window size changes.
Let's start implementing custom hook named useWindowSize that will track the window size. We'll use the addEventListener() method to listen for window resize events.
const useWindowSize = () => {
const [size, setSize] = React.useState({
width: window.innerWidth,
height: window.innerHeight
});
React.useEffect(() => {
const handleResize = () => {
setSize({
width: window.innerWidth,
height: window.innerHeight
});
};
window.addEventListener('resize', handleResize);
return () => {
window.removeEventListener('resize', handleResize);
};
}, []);
return size;
};
Let's see how we can use the useWindowSize hook in a React component:
function App() {
const { width, height } = useWindowSize();
return (
<div>
<p>Window Width: {width}</p>
<p>Window Height: {height}</p>
</div>
);
}
In the useWindowSize hook, we initialize the state with the current window width and height. We then add an event listener for the resize event to update the state whenever the window size changes.
The hook returns an object containing the window width and height.
For performance optimization, You can also use debounce in the handleResize function, this will help in reducing the number of state updates during rapid window resizing.
Anuj Sharma
A seasoned Sr. Engineering Manager at GoDaddy (Ex-Dell) and a frontend tech enthusiast passionate building SaaS application to solve problem. Know more about me 🚀
Be the first to share your thoughts!
No comments yet.
Start the conversation!
Build Your Portfolio
Help the Community
Strengthen Your Skills
Share your knowledge by writing a blog or quick notes. Your contribution can help thousands of frontend developers ace their interviews and grow their careers! 🚀
Anuj Sharma
Last Updated Nov 10, 2025
Find the top React Performance Optimization Techniques specific to React applications that help to make your react app faster and more responsive for the users along with some bonus techniques.
Alok Kumar Giri
Last Updated Jun 2, 2025
Code snippet examples which will help to grasp the concept of Hoisting in JavaScript, with solutions to understand how it works behind the scene.
Anuj Sharma
Last Updated Oct 2, 2025
Explore Polyfill for map, filter and reduce array methods in JavaScript. A detailed explanation of Map, filter and reduce polyfills in JS helps you to know the internal working of these array methods.
Anuj Sharma
Last Updated Nov 10, 2025
Find the step-by-step explanation of the useFetch custom hook in React that helps in fetching the data from an API and handling loading, error states.
Anuj Sharma
Last Updated Nov 11, 2025
Understand step by step how to flatten nested array in javascript using recursion, also explore the flatten of complex array of object.
Anuj Sharma
Last Updated Nov 16, 2025
Implement useThrottle Custom Hook In React (Interview) to limit the number of APi calls to improve the performance of application.
Subscribe to FrontendGeek Hub for frontend interview preparation, interview experiences, curated resources and roadmaps.
© 2025 FrontendGeek. All rights reserved