Implement useWindowSize Hook in React (Interview)
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 Feb 6, 2026

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.
Implementation of useWindowSize() hook
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;
};
Usage
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>
);
}
Explanation
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.
Optimization Considerations
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.
Further Reading 🚀
A seasoned Sr. Engineering Manager at GoDaddy (Ex-Dell) with over 12+ years of experience in the frontend technologies. A frontend tech enthusiast passionate building SaaS application to solve problem. Know more about me 🚀
Learn Next
Featured
100+ Top React JS Interview Questions And Answers
Comments
Be the first to share your thoughts!
No comments yet.
Start the conversation!
Share your expertise
Publish a blog or quick notes on topics you know well — your write-up could be the answer someone needs before their next frontend interview.
Build your portfolio
Help the community
Sharpen your skills
Earn goodies
Other Related Blogs
React Hook Rules: Why hooks declarations are not allowed inside functions
Frontendgeek
Last Updated Feb 6, 2026
A quick guide to explain an important react interview question, why React Hooks declarations are not allowed inside functions or any conditional blocks with code example.
Implementing a stopwatch using React - Frontend Machine Coding Question
Pallavi Gupta
Last Updated Feb 21, 2026
Concise explanation of stopwatch implementation using React, it involves the usage of useEffect hook for creating a stopwatch and tracking milliseconds.
Implement useClickOutside() custom Hook in React [Interview]
Anuj Sharma
Last Updated Dec 23, 2025
Understand the implementation of useClickOutside() custom hook in react and how it can be used to implement Modal like functionality.
Best Frontend System Design Interview Cheat Sheet 📒
Anuj Sharma
Last Updated Jun 9, 2026
A Comprehensive Frontend System Design Cheat Sheet helps you approach the Frontend System Design Interview in the most structured way and covers the 7 most important Frontend System Design Topics.
