Skip to main content
InterviewHubPRO
Section Overview
Q53

Question 53 of 407

General

13%
React Core & Hooks (407 Qs)•General

What are the Pointer Events supported in React?

junior LevelCode ExamplereactGeneral

Pointer Events provide a unified way of handling all input events. In the old days we had a mouse and respective event listeners to handle them but nowadays we have many devices which don't correlate to having a mouse, like phones with touch surface or pens. We need to remember that these events will only work in browsers that support the Pointer Events specification.

The following event types are now available in React DOM:

  1. onPointerDown
  2. onPointerMove
  3. onPointerUp
  4. onPointerCancel
  5. onGotPointerCapture
  6. onLostPointerCapture
  7. onPointerEnter
  8. onPointerLeave
  9. onPointerOver
  10. onPointerOut

Practical Code Example

jsx
// Pointer Events normalize mouse, touch, and pen inputs:
export function DrawingSurface() {
  const handlePointerDown = (e) => {
    e.target.setPointerCapture(e.pointerId);
    console.log(`Captured ${e.pointerType} at coords: (${e.clientX}, ${e.clientY})`);
  };

  return <div onPointerDown={handlePointerDown} className="canvas-surface" />;
}