useState is the simplest React hook out in wild but it has a very interesting feature — lazy initialization
If you provide useState with a function, then it will be executed only on the initial render.
// For every render const [state, setState] = useState(window.localStorage.getItem("key")); // Lazy Initialization - Only once const [state, setState] = useState(() => window.localStorage.getItem("key"));
When to use
Any function that is computationally expensive can be initialized lazily.
Top comments (0)