Modern Full-Stack & React Internals · Chapter 2 · ~15 min · Beginner
State, Interactivity & useState
Bring your user interfaces to life. Learn what state is, why ordinary variables don't update the browser screen, and how the useState hook triggers reactive UI updates.
- Understand why React needs State instead of regular variables
- Declare and update state with the useState() hook
- Handle user click events with clean arrow functions
- Show or hide elements with conditional rendering
1Why Regular Variables Don't Update the Screen
In plain JavaScript, if you want to track a number, you declare a variable: let count = 0;. Then when a user clicks a button, you increment it: count++;.
In React, if you do that, nothing happens on the screen! The number changes in the computer's RAM, but React has no idea the variable changed, so it never redraws the HTML.
To make a screen reactive, you need State. State is a special type of variable that tells React: "Whenever my value changes, automatically redraw this component on the user's screen!"
Why does changing a regular JavaScript variable (like 'let count = 0; count++') fail to update the browser screen in React?
2The useState Hook Explained
To add state to a component, we import and call the useState function:
import { useState } from "react";
function Counter() {
// 1. Declare state with initial value 0:
const [count, setCount] = useState(0);
return (
<div>
<p>Current count: {count}</p>
<button onClick={() => setCount(count + 1)}>
Click to Increment
</button>
</div>
);
}const [count, setCount] = useState(0);
• count: The current value of the state right now.
• setCount: The setter function you call whenever you want to change the value.
• 0: The starting (initial) value when the page first loads.
In 'const [isOpen, setIsOpen] = useState(false);', what is setIsOpen?
3Handling User Clicks & Input
React makes listening to user interactions simple by attaching event listeners directly to JSX tags:
Inline Arrow Function
Great for quick one-line state updates:
<button onClick={() => setCount(count + 1)}>
Add One
</button>Named Handler Function
Clean and readable for multi-step logic:
function handleReset() {
setCount(0);
console.log("Counter reset!");
}
<button onClick={handleReset}>Reset</button>What is the difference between onClick={handleClick} and onClick={handleClick()}?
4Conditional Rendering (Show / Hide UI)
Often you want to show different UI based on whether a user is logged in, whether an item is on sale, or whether a dropdown is open. React uses standard JavaScript logic inside JSX:
1. The Ternary Operator (? :)
Choose between two different UI elements:
{isLoggedIn ? (
<UserProfile user={currentUser} />
) : (
<button onClick={handleLogin}>Log In</button>
)}2. Logical AND (&&)
Render an element ONLY if a condition is true, otherwise render nothing:
{hasUnreadMessages && (
<span className="badge">New Messages!</span>
)}What does '{isAdmin && <AdminDashboard />}' render if isAdmin is false?
5Hands-On: Test State and Re-renders Live
Click the buttons below to interact with real state. Watch how each click updates state in memory and increments the UI Re-render counter:
Interactive useState & Re-render Visualizer
Interactive Component:
0
14 people liked this
Under the Hood (React State Memory):
Corresponding React Code:
// Inside your React Component:
const [count, setCount] = useState(0);
const [liked, setLiked] = useState(false);
return (
<div className="card">
<p>Current Count: {count}</p>
<button onClick={() => setCount(count + 1)}>+1</button>
<button onClick={() => setLiked(!liked)}>
{liked ? "Liked ❤️" : "Like 🤍"}
</button>
</div>
);6Chapter Knowledge Challenge
Test your React state, re-rendering, event handling, and conditional rendering knowledge:
What happens if you modify state directly (e.g. 'count = count + 1;') instead of calling setCount(count + 1)?
What does the expression '{unreadCount > 0 && <span>New notifications!</span>}' render when unreadCount is 0?
7Recap
- State holds data that can change over time and causes React to automatically re-render the screen.
- Declare state using the
useState(initialValue)hook. - Always update state via its setter function (e.g.,
setCount(newValue)), never by mutating variables directly. - Attach user event listeners using camelCase attributes like
onClick, passing the function reference. - Conditionally render UI using the ternary operator (
condition ? <A /> : <B />) or logical AND (condition && <A />).
Congratulations on completing Chapter 2! Mark as complete to update your syllabus: