4 ms·
You can't do that. Just like you can't do: const [count, setCount] = useState(0); count = 1; In both cases, const is const. The fact that `setState`
by sunaurus 6y ago
You can't do that. Just like you can't do:
const [count, setCount] = useState(0);
count = 1;
In both cases, const is const.
The fact that `setState` modifies state outside of the local scope does not mean that a local variable is not const. This is really basic programming, it doesn't have anything to do with React.
Here's the exact same scenario without React to show why you're wrong in your original comment:
const foo = () => {
const bar = getBarFromDatabase();
}
Imagine that this is a typical database where values can change, so that every time `foo()` is called, `bar` can have a different value. Do you think that this is a dirty hack that is overruling the const type declaration as well?
- conmigo 6y ago> This is really basic programming No, it's not. React is iterating that piece of code of your function internally to modify the value of the const, there is no other way to do it. And it is especially confusing because you can use that const 'variable' in the scope representing a changing value. And the change is magically made by its 'setState' method. If you do this yourself in a loop then it is of course totally clear what's going on, but now we're writing functions where parts of it are being iterated by React, and that's not functional at all, IMAO it's a hack.
- sunaurus 6y agoReact does not "iterate over parts" of your functions, it simply calls the entire function. There is no magic happening. You've already received a lot of responses explaining how both const works (which you still seem to misunderstand) and how React works, I hope you can take this as an opportunity to learn and perhaps not be so confident in the future when criticizing something that you clearly haven't taken time to even understand at a basic level.
- hombre_fatal 6y agoI really recommend learning more about what you're complaining about before arguing with people about how you think something works. Your jargon doesn't even make sense. "iterating that piece of code in your function"? Put a console.log() at the top of your function and you'll see that it's simply being called many times.