This is the syntax I'm using.
const [count, setCount] = useState(0); const handleIncrement = () => { setCount((count + 1)); };
I understand that setCount is creating an instance of count, but I really don't understand how it is changed if count is a constant, or how it is called and returns the latest value if it is an instance.
Every time React re-renders the page, doesn't it read the constant count first?
Everything looks normal to me, but I can't understand why.
count
is "constant" during the execution of the function. WhensetCount()
is called, the localcount
will not change. Eventually, your component will re-render with the new values.During this new render pass,
count
will be updated, but it will remain constant during the execution of the render/function.