{"id":1191,"date":"2025-05-21T11:21:46","date_gmt":"2025-05-21T11:21:46","guid":{"rendered":"https:\/\/www.cmarix.com\/qanda\/?p=1191"},"modified":"2026-02-05T12:06:14","modified_gmt":"2026-02-05T12:06:14","slug":"how-to-create-a-custom-hook-that-tracks-mouse-position","status":"publish","type":"post","link":"https:\/\/www.cmarix.com\/qanda\/how-to-create-a-custom-hook-that-tracks-mouse-position\/","title":{"rendered":"How To Create A Custom Hook That Tracks Mouse Position?"},"content":{"rendered":"\n<p>Custom hooks (functions starting with use) encapsulate reusable stateful logic. To track mouse position, use useState to store coordinates and useEffect to add a mousemove event listener to the window. Crucially, the useEffect cleanup function must remove the listener to prevent memory leaks.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { useState, useEffect } from 'react';\n\nfunction useMousePosition() {\n  const &#91;position, setPosition] = useState({ x: 0, y: 0 });\n\n  useEffect(() => {\n    const handleMove = (e) => setPosition({ x: e.clientX, y: e.clientY });\n    window.addEventListener('mousemove', handleMove);\n    \/\/ Cleanup: Remove listener when component unmounts\n    return () => window.removeEventListener('mousemove', handleMove);\n  }, &#91;]); \/\/ Empty array: run effect only once on mount\n\n  return position;\n}\n\n\/\/ Usage in component: const { x, y } = useMousePosition();\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Custom hooks (functions starting with use) encapsulate reusable stateful logic. To track mouse position, use useState to store coordinates and useEffect to add a mousemove event listener to the window. Crucially, the useEffect cleanup function must remove the listener to prevent memory leaks.<\/p>\n","protected":false},"author":2,"featured_media":1193,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[6,3],"tags":[],"class_list":["post-1191","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-react","category-web"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/1191","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/comments?post=1191"}],"version-history":[{"count":2,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/1191\/revisions"}],"predecessor-version":[{"id":1195,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/1191\/revisions\/1195"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/media\/1193"}],"wp:attachment":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/media?parent=1191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/categories?post=1191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/tags?post=1191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}