@@ -13,24 +13,24 @@ function App() {
13
13
const [ seconds , setSeconds ] = useState ( 0 ) ; //set initial seconds played to 0
14
14
const updateClickers = useCallback ( ( ) => {
15
15
//update click multiplier for clickers
16
- setClickMultiplier ( clickers ) ; //set click multiplier
16
+ setClickMultiplier ( ( prevMultiplier ) => clickers ) ; //set click multiplier
17
17
} , [ clickers ] ) ;
18
18
const updateAutoClickers = useCallback ( ( ) => {
19
19
//update points per second for autoclickers
20
- setPointsPerSecond ( autoClickers ) ; //set points per second
20
+ setPointsPerSecond ( ( prevPointsPerSecond ) => autoClickers ) ; //set points per second
21
21
} , [ autoClickers ] ) ;
22
22
useEffect ( ( ) => {
23
23
const interval = setInterval ( ( ) => {
24
24
//increase points every second
25
- setPoints ( points + pointsPerSecond ) ; //increase points by points per second
26
- setSeconds ( seconds + 1 ) ; //add 1 second
25
+ setPoints ( ( prevPoints ) => prevPoints + pointsPerSecond ) ; //increase points by points per second
26
+ setSeconds ( ( prevSeconds ) => prevSeconds + 1 ) ; //add 1 second
27
27
updateClickers ( ) ; //update clickers value
28
28
updateAutoClickers ( ) ; //update auto clickers value
29
29
} , 1000 ) ;
30
30
return ( ) => {
31
31
clearInterval ( interval ) ; //clear interval when component unmounts
32
32
} ;
33
- } , [ points , pointsPerSecond , seconds , updateAutoClickers , updateClickers ] ) ;
33
+ } , [ pointsPerSecond , updateAutoClickers , updateClickers ] ) ;
34
34
35
35
function checkPointsForUpgrade ( points , pointsRequired ) {
36
36
//check if user has enough points to upgrade
@@ -45,22 +45,22 @@ function App() {
45
45
46
46
function addPointsFromClick ( ) {
47
47
//add points from clicking a button
48
- setPoints ( points + clickMultiplier ) ; //increase points by 1 when button clicked
49
- setClicks ( clicks + 1 ) ; //increase clicks made by 1
48
+ setPoints ( ( prevPoints ) => prevPoints + clickMultiplier ) ; //increase points by 1 when button clicked
49
+ setClicks ( ( prevClicks ) => prevClicks + 1 ) ; //increase clicks made by 1
50
50
}
51
51
function upgradeClicker ( ) {
52
52
//upgrade clicker (points per click)
53
53
if ( checkPointsForUpgrade ( points , 10 * Math . pow ( 2 , clickers - 1 ) ) ) {
54
- setPoints ( points - 10 * Math . pow ( 2 , clickers - 1 ) ) ; //spend points
55
- setClickers ( clickers + 1 ) ; //increase clickers by 1
54
+ setPoints ( ( prevPoints ) => prevPoints - 10 * Math . pow ( 2 , clickers - 1 ) ) ; //spend points
55
+ setClickers ( ( prevClickers ) => prevClickers + 1 ) ; //increase clickers by 1
56
56
updateClickers ( ) ; //update clickers value
57
57
}
58
58
}
59
59
function upgradePointsPerSecond ( ) {
60
60
//upgrade points per second
61
61
if ( checkPointsForUpgrade ( points , 10 * Math . pow ( 2 , autoClickers ) ) ) {
62
- setPoints ( points - 10 * Math . pow ( 2 , autoClickers ) ) ;
63
- setAutoClickers ( autoClickers + 1 ) ; //increase autoclickers by 1
62
+ setPoints ( ( prevPoints ) => prevPoints - 10 * Math . pow ( 2 , autoClickers ) ) ;
63
+ setAutoClickers ( ( prevAutoClickers ) => prevAutoClickers + 1 ) ; //increase autoclickers by 1
64
64
updateAutoClickers ( ) ; //update auto clickers value
65
65
}
66
66
}
0 commit comments