Components Re-execute on Every Render
What you'll learn: Why your component function runs from top to bottom every time React needs to update the UI.
The Core Mechanism
When you write a function component, you're creating a template that React calls whenever it needs fresh JSX. Here's the crucial insight: the entire function runs again when props change or (eventually) when state changes. It doesn't just update the parts that changed—it re-executes the whole function body.
function Greeting({ name }) {
console.log('Greeting function is running!');
const message = `Hello, ${name}!`;
return <h1>{message}</h1>;
}
Every time the name prop changes, you'll see that console log fire. The variable message gets recalculated. The JSX gets re-created. Everything runs fresh.
Why This Matters
Think of your component like a vending machine. Each time someone presses a button (a render trigger), the machine doesn't remember what it gave last time—it starts from scratch, reads the current selection (props), and produces a fresh snack (JSX).
This is why you can write straightforward, declarative code:
function UserCard({ user }) {
const displayName = user.firstName + ' ' + user.lastName;
const greeting = user.isLoggedIn ? 'Welcome back' : 'Please log in';
return (
<div>
<h2>{displayName}</h2>
<p>{greeting}</p>
</div>
);
}
You don't manually update displayName when user.firstName changes. The whole function runs again, recalculates everything, and produces new JSX based on the current props.
Key Takeaway: Your component function is a blueprint that React executes completely on every render, producing fresh JSX each time—you describe what the UI should look like right now, not how to update it.