Cursor.jsx:8 Uncaught TypeError: Cannot read properties of null (reading 'clientWidth')
const CustomCursor = () => {
//follows the cursor
const customRef = React.useRef(null)
useEffect(() => {
const onMouseMove = (e) => {
const { clientX, clientY } = e
const mouseX = clientX - customRef.current.clientWidth / 2
const mouseY = clientY - customRef.current.clientHeight / 2
customRef.current.style.transform = `translate3d(${mouseX}px, ${mouseY}px, 0)`
}
// add the event listener
document.addEventListener('mousemove', onMouseMove)
// cleanup function
return () => {
// remove the event listener when the component unmounts
document.removeEventListener('mousemove', onMouseMove)
}
}, [])
return (
)
}
|