React follows a simple but powerful loop:
This cycle ensures the UI always reflects the latest state of the application.
In React, state is managed using hooks, which are special functions that allow components to:
| Hook | Purpose |
|---|---|
| useState | Manage component state |
| useReducer | Manage complex state logic |
| useRef | Persist mutable values without re-render |
| useEffect | Handle side-effects |
| use | Consume async resources (experimental) |
React components go through three main phases:
useEffect(() => {// your side-effect code here.// this is where you can interact with browser APIs for exampledoSomeThing()return function cleanup() {// if you need to clean up after your side-effect (like unsubscribe from an// event), you can do it heredoSomeCleanup()}}, [// this is where dependencies of your useEffect callback go// we'll talk about this in depth in a future exercise.// In this exercise, we'll just leave it as an empty arraydep1,dep2,])
useState(() => initialValue))Triggered by:
Sequence:
useLayoutEffectuseEffect