Codetive
Sign In
Coding challenges as of now are only available on desktop
On mobile you can try our quiz
Quiz
We are working hard to bring coding challenges with editor on mobile 💚

Custom Hook / Window Dimensions

Objective: Implement a custom React hook that returns the current window dimensions (width and height).

Instructions:

  1. Create a custom hook named useWindowDimensions.
  2. The hook should return an object containing the current width and height of the browser window.
  3. The hook should update the dimensions in real-time when the window is resized.

Example Usage:

const { width, height } = useWindowDimensions();
console.log(`Width: ${width}, Height: ${height}`);

Requirements:

  • The custom hook should listen for the resize event on the window and update the width and height values accordingly.
  • The hook returns the correct dimensions when the component mounts.

Hints:

  1. Use useState to store the width and height values.
  2. Use useEffect to handle the window resize event and update the dimensions in real-time.
  3. Remember to remove the event listener when the component unmounts to avoid memory leaks.
Browser
Console
Tests
Soon