Write a Custom React Hook that Returns the Browser's Width and Height

Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 3 years ago

In this lesson, you'll learn how to write a custom React hook that returns the browser's inner width and inner height. We're going to call it useWindowSize.

We first import useState to keep track of the state of the browser window.

We also create a function called getSize( ) that returns an object containing the browser's inner width and inner height from the window object.

We then create a variable called windowSize, a function that will update it called setWindowSize and we set the initial value of our state by calling the getSize( ) function.

Next, we create a function called useWindowSize, and inside of it, we use the useEffect hook.

Inside our useEffect hook, we add an event listener to the window object, that listens to the resize event and will invoke a function called handleResize( ) every time the window is resized. We then return a function that removes this event listener.

The handleResize( ) function updates the state by calling setWindowSize and passing the getSize( ) function to it.

Finally, we return windowSize, which will be an object that contains the browser's inner width and inner height.

Mahmoud Abdelwahab: [0:00] Here we have a React app running, and we're displaying data about the size of the browser window, specifically the inner width and the inner height.

[0:08] We're using a custom React hook called useWindowSize that returns an object containing this information, and we're storing it in a variable called windowSize. To display the width and height we access the object's innerWidth and innerHeight properties. As you can see the values are dynamic and change as we resize the browser window.

[0:32] Let's take a look at how we can build this React hook. We're starting off with an empty JavaScript file called useWindowSize.js. The first thing we'll do is import { useState } from "react". Then we'll create a function called getSize() that will return an object containing the window's innerHeight and innerWidth.

[1:02] Then we'll define a function called useWindowSize(), and inside it, we'll use the useState hook to hold the state of the browser. We do that by defining a variable to hold the state called windowSize, and then we'll define a setter function for updating that state and we'll call it setWindowSize, and we'll retrieve the initial value of our state by calling the getSize function.

[1:24] To listen to the resize event, we'll first need to import the useEffect hook from React. We'll attach an EventListener to the window object listening for the resize event, and trigger a function called handleResize().

[1:44] This function will be called every time the browser's window is resized. Is responsible for setting the state of our hook by calling the setWindowSize function and passing the getSize function to it.

[1:58] Then inside of our useEffect, we'll return a function that removes the EventListener. Since we want this effect to only run when the component mounts, we'll pass an empty array to useEffect, then we'll return windowSize. This is the object that will be returned when we call this hook.

[2:19] To recap, we first imported { useState } from "react". This is to keep track of the state of the browser. We defined a variable called windowSize, and set a function called setWindowSize, and the default value for our state is the getSize function. This is a function that returns the window's innerHeight and innerWidth.

[2:38] Since we want to listen to the resize event, we first imported useEffect and inside useEffect we add an event listener for the resize event, and then every time this event happens, we call handleResize. This function calls the setWindowSize function and then gets the window's inner height and inner width by calling the getSize function that we defined above.

[3:03] Finally, we just remove the event listener and pass an empty array since we want this effect to run once the component mounts. Then, we return windowSize, and this is the value that we return every time we call the useWindowSize hook.

[3:20] Now, we have a custom React hook that returns the window's inner height and inner width.

egghead
egghead
~ an hour ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today