offsetX and offsetY are missing on a React mouse event
event.offsetX is undefinedShort 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.
Fix it fast
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); }} />2Match what you see, then jump to its fix
- 1
e.offsetX is undefined, or TypeScript says the property does not existoffsetX is not part of the React event objectFix - 2
The 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
- 1
What does the error look like?
event.offsetX is undefinedProperty '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
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
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
TypeScript refuses JSX because the jsx option is not set
TS17004: Cannot use JSX unless the '--jsx' flag is provided.
TypeScript compiles JSX only when you name the file with a .tsx extension and enable the jsx compiler option.
2 causes, each with a fixWeb apps
TypeScript rejects a component that returns an array or other non-Element
Its return type 'Element[]' is not a valid JSX element.
TypeScript before 5.1 required every component to return JSX.Element or null, so arrays, strings and Promises were rejected even though React renders them.
2 causes, each with a fixWeb apps
ESLint says it cannot resolve a path to a module that does exist
Unable to resolve path to module 'react'. import/no-unresolved
import/no-unresolved fails when eslint-plugin-import cannot find the file or package on disk with its resolver.
4 causes, each with a fixWeb apps
ESLint reports no-unused-vars for React components and imports that are used
no-unused-vars reported for a component that is rendered as a JSX tag
ESLint's core no-unused-vars does not see variables used only as JSX tags, so a component imported and rendered as <Hello /> looks unused.
3 causes, each with a fixWeb 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 fixWeb apps
The renderInput prop stopped working on the MUI X DatePicker
The custom TextField (variant, size, helperText) is ignored
renderInput was removed from the MUI X date pickers in version 6.
3 causes, each with a fix