Full Stack JS

Chapter 7
0%

Chapter 7: React Hooks

Hooks let you "hook into" React features like state and side effects from function components. They're the modern way to build React apps.

Note: React code can't run in the browser playground, so these are read-only demonstrations. Study the patterns carefully — you'll use them in every React project!
useState
useEffect
useRef
useContext
Custom Hooks
Quiz

useState

useState is like a whiteboard in your room. You write something on it (initial value), and whenever you erase and write something new (call the setter), everyone looking at the whiteboard (the component) sees the update immediately. React re-renders the component every time the whiteboard changes.

useState is the most fundamental hook. It lets a component remember values between renders:

React — useState Basics (Read-Only)

useState Patterns

React — useState Patterns (Read-Only)
Never mutate state directly! Always use the setter function and create new arrays/objects with the spread operator (...). Direct mutation won't trigger a re-render, so your UI won't update.

useEffect

useEffect is like setting an alarm clock. You tell it: "after the component appears on screen (or after something changes), DO THIS." It's for side effects — things that happen OUTSIDE of rendering, like fetching data, starting timers, or updating the document title.
React — useEffect Basics (Read-Only)

The Dependency Array

Dependency ArrayWhen Effect RunsAnalogy
useEffect(fn) — no arrayAfter EVERY renderAlarm that rings every minute
useEffect(fn, []) — empty arrayOnly once (on mount)Alarm that rings once when you wake up
useEffect(fn, [a, b]) — with valuesWhen a or b changesAlarm that rings when specific things change

Cleanup Function

React — useEffect Cleanup (Read-Only)
When to use cleanup: If your effect starts something ongoing (timer, subscription, event listener), always return a cleanup function to stop it. Otherwise, you'll have memory leaks and unexpected behavior.

useRef

useRef is like a sticky note attached to your desk. You can write something on it and it stays there between renders, but changing it does NOT cause a re-render (unlike state). It's also used to directly grab DOM elements, like sticking a name tag on a specific HTML element so you can find it later.
React — useRef (Read-Only)

useRef vs useState

FeatureuseStateuseRef
Triggers re-render?YesNo
Persists between renders?YesYes
Access viaVariable directly.current property
AnalogyWhiteboard (everyone sees changes)Sticky note (private, no announcement)
Use forData that affects UIDOM access, timers, counters that don't affect UI

useContext

Imagine a family announcement system. Without Context, if grandpa wants to tell something to his great-grandchild, the message has to pass through every person in between (parent, child, grandchild). With useContext, grandpa puts the message on a family bulletin board and anyone in the family can read it directly — no passing needed!

Context solves the "prop drilling" problem — passing props through many layers of components:

React — useContext (Read-Only)
Common uses for Context: Theme (dark/light mode), user authentication (logged in user), language/locale, and any data that many components need access to.

Custom Hooks

Custom hooks are like creating your own power tools. Instead of repeating the same sequence of useState + useEffect in every component, you package that logic into a reusable function. The rule: custom hook names must start with use.
React — Custom Hooks (Read-Only)

Hook Rules

Rules of Hooks (never break these!):
1. Only call hooks at the top level — never inside loops, conditions, or nested functions
2. Only call hooks from React function components or custom hooks
3. Custom hook names must start with "use" (e.g., useFetch, useAuth, useLocalStorage)

Popular Custom Hook Ideas

Hook NameWhat It Does
useFetch(url)Fetches data and returns { data, loading, error }
useLocalStorage(key)Syncs state with localStorage
useDebounce(value, delay)Delays updating a value (great for search)
useWindowSize()Tracks browser window dimensions
useAuth()Manages user authentication state

📝 Chapter 7 Quiz

1. When does useEffect(() => {}, []) run?

After every render
Only once, when the component mounts
Never — empty array means disabled
Before the component renders

2. What's the key difference between useRef and useState?

useRef is faster
useState can only hold numbers
Changing useRef does NOT trigger a re-render
useRef can only be used once per component

3. What problem does useContext solve?

Prop drilling (passing props through many layers)
Slow rendering
Memory leaks
API rate limiting

4. Which of these is NOT allowed with hooks?

Using multiple useState in one component
Creating custom hooks
Using useEffect with a dependency array
Calling useState inside an if-statement

5. What must a custom hook's name start with?

hook
use
custom
react
← Chapter 6: React Fundamentals Chapter 8: React Router →