Question
Solution
Tests
Intermediate
React

Window Dimensions Hook

Write a custom hook useWindowDimensions that returns an object with two properties: the current width and height of the window. The hook should update the dimensions whenever the window is resized. Please see the example below for the required return object.

We have already written some starting code for you.

Hide Demo
x

Directions

  • You do not need to edit the App component. Tests will be scoring only the useWindowDimensions hook.

Example

{
width: number,
height: number
}

Need some hints?

Make use of the built-in useState and useEffect hooks to track the window dimensions and update them when the window is resized.

The useEffect hook should have an event listener to listen for a resize event and call the setWindowDimensions function to update the state with the new dimensions.

Remember to cleanup the event listener by returning a function from the useEffect hook that removes the listener, this helps prevent memory leaks in your application.

Loading...
Preview
Console
Tests
SHOW
Feedback
Format
Reset
Run tests
--> There are no logs to display