How is it possible that following function after rerender keeps the current value?
const Example = () => {
const [count, setCount] = useState(0);
return <button onClick={()=>setCount(count+1)} >{count}</button>
}
Logically looking:
- initial render = we call useState with an argument
- rerender = we call useState again with an argument
Looking at the component as a clean function, this should work. But as I understand it, useState changes pure component into impure component.
But this still doesn't explain implementation of such a mechanism in an arrow function that does not have its own context. useState doesn't supposed to know which time in a row it's being called, if it's not hooked to parent function (like unique ID, this, callback, etc.)
React Hooks look like they break many paradigms of functional programming...
useStateends up accessing a global variable that contains the hook data for that component instance. - Brandon