React · Lesson 2 of 15 · 3 min
JSX, Functional Components & Props
This is where you prove you understand React's declarative nature and one-way data flow.
JSX is syntactic sugar, not a template language
JSX looks like HTML, but it's not. It's a syntax extension that transpiles directly to React.createElement() function calls. <div>Hello</div> becomes React.createElement('div', null, 'Hello').
This knowledge isn't trivia; it explains React's rules.
- Single Root Element: A function can only have one return value. That's why your component's JSX must be wrapped in a single element or a Fragment (
<>...</>). - JS Keywords: Attributes like
classandforbecomeclassNameandhtmlForbecause they are reserved keywords in JavaScript, and JSX attributes become keys in a JavaScript object. - Expressions: You can embed any JavaScript expression inside
{}because you're already inside JavaScript.
In an interview, when asked "What is JSX?", don't just say "it's like HTML in JavaScript." A better answer: "JSX is syntactic sugar for the
React.createElementfunction. It lets us write declarative UI that looks like HTML, but with the full power of JavaScript. It transpiles to plain JavaScript objects that React uses to build the DOM."
Components are just functions
In modern React, your components are JavaScript functions that take one argument—props—and return JSX. That's it.
// A component is a function that returns UI.
// It receives data via a 'props' object.
const UserProfileCard = ({ name, bio, children }) => {
return (
<div className="card">
<h2>{name}</h2>
<p>{bio}</p>
{/* 'children' is a special prop for nested content */}
<div className="card-actions">
{children}
</div>
</div>
);
};
// A parent component uses it and passes props.
const App = () => {
return (
<main>
<UserProfileCard
name="Alex"
bio="Frontend developer specializing in React."
>
<button>Contact</button>
<button>View Portfolio</button>
</UserProfileCard>
</main>
);
};
This simplicity is the point. They are easy to read, easy to test (input props, assert output JSX), and don't carry the baggage of this context like old class components.
Props enforce one-way data flow
Props are the mechanism for passing data from parent to child. This flow is strictly unidirectional. A child component receives props but can never modify them.
This one-way data flow is a core principle of React. It makes your application predictable. When you see a bug, you can trace the data down the component tree from its source. If a child could modify its props, data could change anywhere, making debugging a nightmare.
The trap: Trying to change a prop inside a child component, like props.name = 'new name'. This will throw an error in strict mode and reveals a fundamental misunderstanding of React's architecture. If a child needs to communicate a change upwards, the parent passes down a callback function as a prop.
The special children prop
The children prop isn't magic. It's just a property that holds whatever JSX you nest inside a component's opening and closing tags.
Look at the UserProfileCard example again. The two <button> elements inside <UserProfileCard>...</UserProfileCard> are passed as props.children. This is React's primary composition model. It lets you create generic "wrapper" components like cards, modals, or layouts that don't need to know what they will contain.
Try it
Create a Badge component. It should accept a color prop (a string like 'red' or '#00ff00') and a children prop. Use the color prop to set the backgroundColor via an inline style object. The component should render the children inside a <span>.
Next: Now that you can pass data down, we'll learn how components manage their own data with useState and respond to side effects with useEffect in "State Management with useState & useEffect".
Lock it in — apply this lesson under interview conditions.