FrontendArk

Master Frontend Interviews

  • Practice
  • Playground
  • Resource
Medium

Use Hover

20mins

Implement a hook useHover(element) that tracks whether a given DOM element is currently being hovered.

The hook should:

  • Accept a DOM element reference.
  • Return a boolean indicating if the element is being hovered.
  • Automatically handle attaching and removing mouseenter and mouseleave events.
  • Update the state whenever the hover status changes.

Example

const ref = document.getElementById('button'); const isHovered = useHover(ref); console.log(isHovered); // true when mouse is over the element, false otherwise

Code Editor

JavaScript Function

00:00
Loading...

Test Cases

No test results yet

Click "Run" to execute tests