According to the official React docs on useState,
If the initial state [to be passed to useState] is the result of an expensive computation, you may provide a function instead, which will be executed only on the initial render
My question: I thought that the expression [myState, setMyState] = useState('starting value')
was always only executed on initial render; if it were executed more than once, wouldn't that set the value of myState
back to starting value
?
I don't see why lazy initialization is needed unless useState
get called more than once, and doing that seems to undermine the idea of preserving state between renders.
Thanks!