Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

offsetX and offsetY are missing on a React mouse event

Errorevent.offsetX is undefined

Short answer

React's mouse event object lists clientX, pageX and screenX, but not offsetX or offsetY. The browser's own event still has them, and React hands it to you as event.nativeEvent. Be careful, though: offsetX is measured from the element under the pointer, not from your handler's element.

  • Applies to React 19, TypeScript 5
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Log the synthetic event and its native event side by side. If offsetX shows up only on the second one, you are in the first cause below. If it shows on both but the numbers look wrong, go to the second.

    <canvas
      onMouseMove={(e) => {
        console.log(e.clientX, e.nativeEvent.offsetX);
      }}
    />
  2. 2Match what you see, then jump to its fix

    1. 1e.offsetX is undefined, or TypeScript says the property does not existoffsetX is not part of the React event objectFix
    2. 2The value is right on empty areas but resets to a small number when the pointer is over a child, such as an image, a span or an SVG shapeoffsetX is measured from the element under the pointer, not from your handler's elementFix

What does the error look like?

  • event.offsetX is undefined
  • Property 'offsetX' does not exist on type 'MouseEvent<HTMLCanvasElement, MouseEvent>'
  • offsetX jumps to small numbers when the pointer moves over a child element

Cause 1 of 2

offsetX is not part of the React event object

1Check

React's documentation for mouse handlers lists altKey, button, buttons, clientX, clientY, movementX, movementY, pageX, pageY, screenX, screenY and a few more. offsetX and offsetY are not on that list. The original browser event is available as nativeEvent.

<div onClick={(e) => {
  console.log('offsetX' in e.nativeEvent); // true on the DOM event
  console.log('offsetX' in e);                                 // false on the React event
}} />

2Fix

Read the value from e.nativeEvent. In TypeScript the type of nativeEvent is the DOM MouseEvent, so the property is typed and no cast is needed.

function onMove(e: React.MouseEvent<HTMLCanvasElement>) {
  const x = e.nativeEvent.offsetX;
  const y = e.nativeEvent.offsetY;
  draw(x, y);
}

3Verify

Move the pointer over the element. The logged numbers change with the pointer and are not undefined.

console.log(e.nativeEvent.offsetX, e.nativeEvent.offsetY);

Confirmed by react.dev

Did this fix it?

Cause 2 of 2

offsetX is measured from the element under the pointer, not from your handler's element

1Check

MDN defines offsetX as the distance from the pointer to the padding edge of the event's target node. The target is the innermost element under the pointer, which is not always the element that owns your handler. Compare the two in the log.

onMouseMove={(e) => {
  console.log(e.target === e.currentTarget, e.nativeEvent.offsetX);
}}

2Fix

Compute the position from the handler's own element. getBoundingClientRect() returns the element's box in viewport coordinates, so subtracting its left and top from clientX and clientY gives a position that does not depend on which child is hit. Note that this box includes the border, so subtract the border widths too if the element has one.

function onMove(e: React.MouseEvent<HTMLDivElement>) {
  const rect = e.currentTarget.getBoundingClientRect();
  const x = e.clientX - rect.left;
  const y = e.clientY - rect.top;
  setPoint({ x, y });
}

3Verify

Move the pointer across a child element inside the container. The numbers now change smoothly and never jump back to zero.

console.log(e.clientX - rect.left, e.clientY - rect.top);

Confirmed by developer.mozilla.org

Did this fix it?

How do I stop it coming back?

  • Use clientX and clientY with getBoundingClientRect() for positions inside a container; use nativeEvent.offsetX only when the target is the element you care about.
  • Set pointer-events: none on decorative children if they should not become the event target.
  • Keep the handler on the element you measure, and read e.currentTarget rather than e.target.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

TypeScript rejects MUI Data Grid columns that set a type

A TypeScript error inside a column's renderCell callback

A columns array declared without a type is inferred with type as a plain string, which the Data Grid's column type union does not accept.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.