When you pass a function as an argument to useState instead of a "regular" value, it is only run on the initial render. It’s a way React offers to do initialization work only once, instead of on each render.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hi @selbekk , nice article! Can you explain a little about the last section(about optimization)? How would
() => JSON.parse(localStorage.getItem(key))
inside useState() work? Wouldn't it not work, since it is declared, not invoked inside useState()?
Thanks!
When you pass a function as an argument to useState instead of a "regular" value, it is only run on the initial render. It’s a way React offers to do initialization work only once, instead of on each render.