State Management · Junior · Question 6 of 75
Why pass a function to useState(() => createInitialTodos())?
- ✓Correct answer: So the expensive computation runs only on the first render
- 2So the state becomes read-only after initialization
- 3So the initial value is loaded asynchronously from the server
Explanation
useState(createInitialTodos()) calls the function on every render, even though the result is used only once. A lazy initializer useState(createInitialTodos) is called only on mount. It must be synchronous and pure.