Hacker News new | ask | show | jobs
Ask HN: How come, const variable can be modified in react
1 points by dee8717 1384 days ago
I can modify const variable in react using setSate but not in vanilljs. Any reason ?
2 comments

When you have something like `const [variable, setVariable] = setState('foobar')`, you are not getting "variable" back directly. Instead, you're getting a pair of a setter function and a getter function. React keeps track of the variable's value internally, and when you access `variable` you're really just calling a getter function that "requests" the value from React, essentially. So you can can call `setVariable` to modify the value internally to React without actually changing the binding of the variable you see, hence it can be const.
Thanks, that makes some sense.
It's very unclear what you mean, but I suspect the answer is something along the lines of "you are not modifying the variable, you are creating a new one with the same name"