Why pass a function to useState for expensive initial values?
Source: react.devintermediate

This tests your understanding of React's render-phase behavior. Without the function, expensive work reruns on every render and is thrown away; the initializer runs once only during mount.
This tests your understanding of React's render-phase execution model and how useState initializes state. A strong answer explains that passing a raw value causes the expensive computation to execute on every render even though React discards the result after the first; passing an initializer function guarantees React calls it only once during component mount.
Read the original → react.dev
- #react
- #usestate
- #hooks
- #performance
- #initialization
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.