Interacting with APIs is the heart of modern web apps. React provides the tools to manage this complexity cleanly.
1The Tripartite State
When fetching data, you aren't just managing 'data'. You are managing a lifecycle. A professional component always handles three distinct states: 1. Loading (the request is in flight), 2. Error (the request failed), and 3. Success (the data arrived). Failing to handle any of these leads to a poor user experience, like a blank screen or a silent crash.
2Effects as Synchronization
Think of useEffect not as a 'lifecycle' method, but as a 'synchronization' mechanism. You are synchronizing your component's state with an external system (the API). By using an empty dependency array, you tell React that this synchronization only needs to happen onceβwhen the component first appears on the screen.
