JSX and components
Understand the idea
You already know HTML tags and JavaScript . A React component is a function that returns tags. JSX is the spelling for those tags inside JavaScript.
Start from what you already know
You already write HTML for structure and JavaScript for behavior. React combines them: you write a JavaScript , and that function returns markup that looks like HTML. That markup-in-JavaScript spelling is called JSX.
A component is that function. The name starts with a capital letter. You use it like a custom tag, for example Greeting. When the page needs that piece of UI, React calls the function and puts the return value on the screen.
Open the live example. App is the outer component. Greeting is a smaller one inside it. Read App first, then Greeting.
JSX rules that trip people up
JSX looks like HTML, with a few important differences:
- A component must return one parent. You cannot return two sibling tags side by side with nothing wrapping them. Wrap them in one element, or in a fragment written as an empty pair of angle brackets.
- To put a JavaScript value in the markup, use curly braces around the value, for example the variable name. Inside those braces you write an expression (a value), not a full if statement.
- Use className instead of class. In JavaScript, class is a reserved word, so JSX renamed the attribute.
- Use htmlFor instead of for on a label, for the same reason.
| In HTML | In JSX | Why it changed |
|---|---|---|
| class="card" | className="card" | class is reserved in JavaScript |
| for="email" | htmlFor="email" | for is reserved |
| Hello | Hello plus the name in curly braces | Curly braces insert a JavaScript value |
| HTML comment syntax | A JavaScript comment inside curly braces | Comments use JavaScript comment syntax |
Props are the inputs
A is an argument you pass into a component, the same way you pass an argument into a function.
In the example, App writes a Greeting tag and passes name set to Ada. Greeting receives that as a prop named name. The Greeting function parameter list destructures name and children so you can use them as normal variables.
children is special: it is whatever you put between the opening and closing tags. In the example, that is the Welcome back or Please sign in paragraph.
Conditional rendering means choosing what to return. The example uses a ternary: if signedIn is true, show Welcome back; otherwise show Please sign in. Both branches are JSX. Only one is returned.
Class components, briefly
In older code you may see class Greeting extends React.Component. That style used this.props and this.state. You should be able to recognize it when you meet it. For new work in this track, write function components. Hooks (starting in the next lessons) are how function components keep state and run effects.
How to read the live example
- App runs and prints five console lines that name the ideas of this lesson.
- App returns a Greeting with name Ada and some children inside it.
- Greeting returns a section with className set to card, a heading Hello, Ada, and the children from App.
- The preview shows that heading. The console shows the five labels.
You do not change the page with document.querySelector here. You return the next description of the UI, and React updates the page. Later lessons cover state and effects. For now, the skill is: function in, JSX out, props as inputs.
Mental model
FOLLOW THE VALUE
Render calculates the next tree.
01 / 03Trace the transformation
Live example
Read App from top to bottom. It logs five labels, then returns a Greeting with the name Ada. Greeting prints Hello, Ada and shows either Welcome back or Please sign in.
export default function App() { console.log("Hello, Ada"); // Greeting the preview will show. console.log("one-root"); // A component returns one root value. console.log("className"); // JSX uses className, not class. console.log("props"); // Inputs arrive as props. console.log("function-component"); // This track uses function components. const signedIn = true; // A value the condition reads. return ( <Greeting name="Ada"> {/* Pass a prop into Greeting. */} {signedIn ? <p>Welcome back</p> : <p>Please sign in</p>} {/* Choose a branch. */} </Greeting> ); } function Greeting({ name, children }) { // Destructure props, including children. return ( <section className="card"> {/* className becomes the DOM class. */} <h1>Hello, {name}</h1> {/* Curly braces insert a value. */} {children} {/* Nested content from the parent. */} </section> ); }
VISUAL FLOW
Try the same flow interactively
A component is a function. Props go in. JSX comes out. React puts that result on the page.
Click through props → JSX → screen
What the user sees
…
React calls App
export default function App() {React starts the outer component. Everything below runs because App was asked to render.
Guided challenge
Name the attribute React actually reads
The component logs class. Change the log so it records className, the attribute JSX uses for a CSS class.
export default function App() { console.log("Hello, Ada"); // Keep the greeting log. console.log("one-root"); // Keep the one-root log. console.log("class"); // Wrong label - change this to className. console.log("props"); // Keep the props log. console.log("function-component"); // Keep the component-style log. return ( <section className="card"> {/* The attribute is already className. */} <h1>Hello, Ada</h1> </section> ); }
EXPECTED CONSOLE OUTPUT
Hello, Ada one-root className props function-component
Hints 0/2
Free sandbox
Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.
export default function App() { return ( <section className="card"> {/* Try changing the className. */} <h1>Hello, Ada</h1> {/* Try a different name. */} <p>Change the name, the condition, or the markup.</p> </section> ); }
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 100 XP. You can always revisit it.