State Management · Junior · Question 6 of 75

Why pass a function to useState(() => createInitialTodos())?

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.

Further reading

Problem with a question

State Management · Junior · question 6

Test yourself

Take the State Management · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz