React Tutorial #7: useState and useEffect
In the previous tutorial, you learned conditional rendering. Now let’s go deeper into useState and useEffect — the two hooks you will use in almost every React component. useState In Depth You have already seen useState basics. Let’s cover the advanced patterns. Lazy Initialization If the initial state value is expensive to compute, pass a function: // ❌ Runs getInitialTodos() on every render const [todos, setTodos] = useState(getInitialTodos()); // ✅ Runs getInitialTodos() only once const [todos, setTodos] = useState(() => getInitialTodos()); The function is only called on the first render. ...