Question · JavaScript and React course · Module 3: State and Events
useState Initialization
How do you pass an expensive initializer function to useState so it only runs once?
Answer options
- useEffect(() => setState(computeExpensiveValue()), [])
- useState(computeExpensiveValue()) - calling the function directly
- useState(() => computeExpensiveValue()) - passing an initializer function
- useMemo(() => computeExpensiveValue(), []) instead of useState
You can check the correct answer by solving this task in the CodeWorlds game.