Skip to content
Justice Tech School

Lazy Initial State

Pass a function to useState to run expensive setup only once.

Example
const [data] = useState(() => JSON.parse(localStorage.getItem("d") || "[]"));

Exercise

Function initialiser.

useState(()  0)

Quiz Test

When does a lazy initialiser run?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.