Contents of React Interview Questions

Comprehensive collection of React interview questions and answers covering hooks, components, state management, and best practices.

What Pointer Events are supported in React?

In React, Pointer Events provide a way to handle mouse, touch, and pen interactions consistently across devices. React implements a wrapper around the native Pointer Events API for better cross-browser support and event normalization.

React supports the following pointer events:

Event NameDescription
onPointerDownFired when a pointer (mouse, touch, pen) is pressed down.
onPointerMoveFired when a pointer moves across the screen.
onPointerUpFired when a pointer is released (mouse button up, touch ends, etc.).
onPointerCancelFired when the pointer event is canceled (e.g., by the browser).
onPointerEnterFired when a pointer enters the boundaries of an element (similar to onMouseEnter).
onPointerLeaveFired when a pointer leaves the boundaries of an element (similar to onMouseLeave).
onPointerOverFired when a pointer is moved onto an element or one of its children.
onPointerOutFired when a pointer is moved off an element or one of its children.
onGotPointerCaptureFired when an element captures a pointer using setPointerCapture().
onLostPointerCaptureFired when an element loses pointer capture.

Key Points to Remember:

  • Pointer events are more powerful than regular mouse or touch events because they work across different input types (mouse, touch, pen).
  • They can help reduce the need for separate handling of onMouseDown, onTouchStart, etc.
  • React Synthetic Events wrap native pointer events to ensure cross-browser compatibility.

Example:

function PointerExample() { const handlePointerDown = (event) => { console.log('Pointer down:', event.pointerType); }; return ( <div onPointerDown={handlePointerDown} style={{ width: 200, height: 200, backgroundColor: 'lightblue' }} > Click, touch, or use a pen here! </div> ); }

In this example, no matter if the user clicks with a mouse, touches the screen, or uses a pen — the same event handler will be triggered!