1

i have already posted questions here regarding my humble attempt at creating a space invader type of game !
this time i am having troubles with useEffect rerendering everytime i shoot a laser, leading to launching a setintervall again everytime i hit shoot, and then my lasers are going faster and faster !! is the solution placing my setInterval elsewhere, or working on the useEffect so it does not render when i shoot a laser ?? here is my updated sandbox :

https://codesandbox.io/s/mfcqd?file=/src/MainGameContainer.jsx

i am guessing the probleme lies in './src/gameElements/lasers.jsx' and it's the useEffect at the beggining of the function that keeps on rendering everytime i shoot.

thank you if anaone can help !!

1 Answer 1

2

You have to clear the interval in your useEffect cleanup, otherwise when component re-rerenders, the old interval is still there:

 useEffect(() => { let timing = setInterval(moveLasers, 30); return () => clearInterval(timing); }); 
Sign up to request clarification or add additional context in comments.

1 Comment

oh yes i tried that but probably did it the wrong way, thsk so much !!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.