I have the following code in React:
let down = 0
const moveUp = e => {
//other code
let up = down - 300
}
const moveDown = e => {
//other code
down = down + 300
}
<Button onClick={moveUp} btntext="Up"/>
<Button onClick={moveDown} btntext="Down"/>
Down is the first button a user would click on, it increments the down variable by 300, so let's say we've incremented down 3 times, which would be 900.
The problem is, when we try to click on the Up button referencing the moveUp
function, the down variable is now set back to it's default which is 0, why does that happen, shouldn't the change to 900 be stored in memory?
I would really love some help here, there's clearly something I'm not understanding about Javascript events and variable scopes.