Blog/NotesConcept

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.

Intermediate

Anuj Sharma

Last Updated Feb 6, 2026


Implement useWindowSize Hook in React (Interview)

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 🚀

  1. Top 20 Most Asked Custom Hooks in React
  2. Best Resources to Ace React Interviews

🚀

Love this content? Share it!

Help others discover this resource

About the Author

Anuj Sharma

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  🚀

Comments

Be the first to share your thoughts!

Guest User

Please login to comment

0 characters


No comments yet.

Start the conversation!

Share Your Expertise & Help the Community!

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! 🚀


Other Related Blogs

Implement useFetch() Custom Hook in React (Interview)

Anuj Sharma

Last Updated Feb 21, 2026

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.

setTimeout Polyfill in JavaScript - Detailed Explanation

Anuj Sharma

Last Updated Aug 3, 2025

Explore the implementation of setTimeout in JavaScript with a detailed explanation for every step. Understand all scenarios expected to implement the setTimeout polyfill.

Implement useSessionStorage() Custom Hook in React [Interview]

Anuj Sharma

Last Updated Nov 15, 2025

Understand the code implementation of useSessionStorage custom hook in react that will help to efficiently manager session storage in application.

Top 10 React Performance Optimization Techniques [React Interview]

Anuj Sharma

Last Updated Feb 21, 2026

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.

Implement useToggle() Custom Hook in React (Interview)

Anuj Sharma

Last Updated Feb 21, 2026

Explore code explanation of useToggle() custom hook in react to handle the toggle event efficiently.

21 Most Asked Frontend System Design Interview Questions & Patterns

Anuj Sharma

Last Updated Dec 17, 2025

A comprehensive collection of the most asked Frontend System Design Interview Questions for Experienced along with the related Answers, Patterns and Important Resources.

Stay Updated

Subscribe to FrontendGeek Hub for frontend interview preparation, interview experiences, curated resources and roadmaps.

FrontendGeek
FrontendGeek

All in One Preparation Hub to Ace Frontend Interviews. Master JavaScript, React, System Design, and more with curated resources.

Consider Supporting this Free Platform

Buy Me a Coffee

Product

HomeFrontend InterviewInterview ExperienceBlogsToolsLeaderboard

Tools

CSS Image FilterPixelate ImageAspect Ratio CalculatorBox Shadow GeneratorCSS Gradient GeneratorNeumorphism GeneratorExplore More Tools →

© 2026 FrontendGeek. All rights reserved