score:0
Accepted answer
You're voilating several rules while using React Component and hooks, here's a much standard version of your code:
// Put the variables outside to make sure they won't reset while component refresh
let mouseX = 0
let mouseY = 0
let ballX = 0
let ballY = 0
let speed = 0.2
const Button = props => {
const ballRef = React.useRef(null);
// use useCallback to make sure this function won't create multiple copies when refreshed
const animate = React.useCallback(() => {
// Make sure the ballRef is created, at the first run, the ball ref is still null
if(ballRef && ballRef.current) {
let distX = mouseX - ballX
let distY = mouseY - ballY
ballX = ballX + distX * speed
ballY = ballY + distY * speed
ballRef.current.style.left = ballX + "px"
ballRef.current.style.top = ballY + "px"
}
requestAnimationFrame(animate)
}, [ballRef, mouseX, mouseY, ballX, ballY]);
// This make sure the function inside only runs when the component is mounted
React.useEffect(()=>{
const onMouseMove = event => {
mouseX = event.pageX;
mouseY = event.pageY;
};
document.addEventListener("mousemove", onMouseMove);
animate();
// When the component is unmount, remove the listener
return () => document.removeEventListener("mousemove", onMouseMove);
}, []);
return (
<div ref={ballRef} >Button</div>
)
}
export default Button
score:0
You need to set animate()
in useEffect
useEffect(() => {
animate();
}, []);
Source: stackoverflow.com
Related Query
- Getting a Cannot read property 'style' of null Error When Using useRef Within a React Functional Component
- When using getDerivedStateFromProps (React) getting error: Cannot read property 'setState' of null
- Getting the error as "TypeError: Cannot read property 'file' of undefined" when uploading a video using Multer
- Error using useeffect and useref TypeError: Cannot read property 'getBoundingClientRect' of null
- ReactJS: Cannot read property 'value' of null when getting selected value
- How to fix 'TypeError: Cannot read property 'style' of undefined' error when using material-ui Transitions
- React, Jest, and Testing Library: TypeError: Cannot read property 'createEvent' of null when using findBy such as findByAltText
- Getting error TypeError: Cannot read property 'id' of undefined using React/Redux action
- Error when trying to map a choropleth using react-leaflet-choropleth: Cannot read property 'map' of undefined
- I'm getting a Cannot read property of 'exercises' of undefined error when trying to map my props
- Keep having the error "TypeError: Cannot read property 'map' of undefined" when using map function in React js
- TypeError: Cannot read property 'offsetHeight' of null when using the Locomotive Scroll react module
- Cannot read property 'focus' of null error when extracting component
- Getting error bundle.js:39454 Uncaught TypeError: Cannot read property 'email' of undefined using MERN stack
- TypeError: Cannot read property 'offsetHeight' of null after screen resize when using React.createRef
- Getting this error , when trying to upload images through a form "TypeError: Cannot read property 'originalname' of undefined "
- Cannot read property 'map' of undefined Error when using axios.get inside useEffect hook
- TypeError: Cannot read property 'setTransform' of null when using Openlayers and Jest + Enzyme
- I'm getting this error when fly over the chart: TypeError: Cannot read property 'lineWidth' of undefined
- Why am I getting error 'Cannot read property 'name' of null' when using a parsed string?
- Getting Jest error TypeError: Cannot read properties of undefined (reading 'match') when using useParams in the component?
- getting the error Cannot read property 'value' of undefined in react while using arrow function
- I am trying to match a string with particular word, but i am getting an error cannot read property input of null in else
- Getting Error TypeError: Cannot read property 'rows' of null
- Getting "Uncaught TypeError: Cannot read property 'results' of undefined " when mapping the data from API using Redux
- TypeError when using React: Cannot read property 'firstChild' of undefined
- Cannot read property 'refs' of null react error react js
- TypeError: Cannot read property 'match' of undefined when using useParams from react-router
- Error : Cannot read property fetch. Using jest-expo with react native
- Cannot read property of undefined when using react hooks
More Query from same tag
- Cannot read property 'publish' of undefined in react
- How to use React Native launch options parameter for RCTRootView
- React state overwrites the current reducer
- React render Table with two arrays
- How to add an element in the your index inside a array? ReactJs
- is it possible to custom the image size of dangerouslySetInnerHTML in react
- Redux-persist is not working: Page refresh clears the state
- My React App never loads in localhost:3000 its just always spinning (Think React-Router-Dom is the issue)
- Style sheet is not styling React Component
- array.list.concat() is removing two elements in json
- Delete firebase document using field value
- I cannot change the array in State
- React-bootstrap Popover: Not positioned where I expect it
- Check a div value and update a class in React
- what is the best way to use react router V6 navigation with redux and redux thunk actions?
- Error while running reactjs app under pm2
- How to mock variable in Jest?
- Switch over to Create React App on Existing Project?
- What is the best way to build multi-language app with react and redux?
- How to translate a tsx file to a js file for React
- render link tag in react object
- Using FontAwesome inside a ReactJS component
- Apollo GraphQL Subscriptions
- Move Chevron in Fluent UI Nav to Right Side
- React.memo with react-router-dom useLocation()
- Changing react-router v3 to react-router v4
- How to not fire throttle function from lodash?
- How to setup Firebase Cloud Messaging v9 in React?
- Check if array undefined in React component
- Can't import other project's component in create-react-app with craco