Skip to content
beginnerPhase 36 · React

JSX

Write JSX syntax, embed expressions, and understand JSX compilation.

30m
0 problems
Topic Progress0%

JSX Syntax

JSX Syntax

JSX is a syntax extension for JavaScript that looks like HTML but compiles to JavaScript function calls.

Basic JSX

// JSX element
const element = <h1>Hello, World!</h1>;

// JSX with attributes
const element = <img src="photo.jpg" alt="Photo" />;

// Nested JSX
const element = (
  <div>
    <h1>Title</h1>
    <p>Paragraph</p>
  </div>
);

JSX is Expressions

JSX compiles to JavaScript expressions:

// This JSX:
const element = <h1>Hello, {name}!</h1>;

// Compiles to:
const element = React.createElement("h1", null, "Hello, ", name, "!");

JSX Must Have One Root

// Good: Single root element
function App() {
  return (
    <div>
      <h1>Title</h1>
      <p>Content</p>
    </div>
  );
}

// Also good: Fragment
function App() {
  return (
    <>
      <h1>Title</h1>
      <p>Content</p>
    </>
  );
}

Expressions in JSX

Expressions in JSX

Use curly braces {} to embed JavaScript expressions in JSX.

Variables

const name = "Alice";
const element = <h1>Hello, {name}!</h1>;

// Works with any expression
const element = <h1>Hello, {"Alice"}!</h1>;
const element = <h1>{2 + 2}</h1>;
const element = <h1>{isLoggedIn ? "Welcome" : "Please log in"}</h1>;

Function Calls

function formatName(user) {
  return user.firstName + " " + user.lastName;
}

const element = <h1>Hello, {formatName(user)}!</h1>;

Objects

const style = { color: "red", fontSize: "20px" };
const element = <p style={style}>Red text</p>;

// Inline styles
const element = <p style={{ color: "red" }}>Red text</p>;

Arrays

const items = ["apple", "banana", "cherry"];
const element = (
  <ul>
    {items.map((item) => (
      <li key={item}>{item}</li>
    ))}
  </ul>
);

Conditional Rendering

// Ternary
const element = <div>{isLoggedIn ? <Dashboard /> : <Login />}</div>;

// Logical AND
const element = <div>{messages.length > 0 && <MessageList />}</div>;

JSX Gotchas

JSX Gotchas

className vs class

// Wrong: class is a reserved word in JavaScript
const element = <div class="container">Content</div>;

// Correct: use className
const element = <div className="container">Content</div>;

htmlFor vs for

// Wrong
const element = <label for="name">Name</label>;

// Correct
const element = <label htmlFor="name">Name</label>;

Self-Closing Tags

// Wrong: HTML allows this
const element = <img src="photo.jpg">;

// Correct: JSX requires self-closing
const element = <img src="photo.jpg" />;
const element = <input type="text" />;
const element = <br />;

Style as Object

// Wrong: CSS string
const element = <p style="color: red">Red</p>;

// Correct: style object
const element = <p style={{ color: "red" }}>Red</p>;

Event Handlers

// Wrong: string
const element = <button onClick="handleClick()">Click</button>;

// Correct: function reference
const element = <button onClick={handleClick}>Click</button>;

// Correct: arrow function for inline
const element = (
  <button onClick={() => console.log("clicked")}>
    Click
  </button>
);

Fragments

// Wrong: returns two elements
function App() {
  return (
    <h1>Title</h1>
    <p>Content</p>
  );
}

// Correct: use Fragment
function App() {
  return (
    <>
      <h1>Title</h1>
      <p>Content</p>
    </>
  );
}

Practice Problems

0/3solved
Build JSX Component

Create a reusable React component implementing JSX. Include proper state management and accessibility.

Solution
// Production-ready component with:
// - Proper TypeScript types
// - Accessibility (ARIA)
// - Error boundaries
// - Loading states
// - Memoization where needed
JSX Testing

Write unit and integration tests for JSX using React Testing Library.

Solution
// Test coverage:
// 1. Rendering tests
// 2. Interaction tests
// 3. Edge case tests
// 4. Accessibility tests
JSX Performance

Optimize JSX for performance. Consider memoization, code splitting, and bundle size.

Solution
// Optimization techniques:
// 1. React.memo / useMemo / useCallback
// 2. Code splitting with lazy()
// 3. Virtual scrolling for lists
// 4. Image lazy loading
// 5. Bundle analysis

Quiz

1. What does JSX stand for?

Question 1 options

2. How do you embed a JavaScript expression in JSX?

Question 2 options

3. What should you use instead of 'class' in JSX?

Question 3 options

4. How do you return multiple elements from a component?

Question 4 options

Flashcards

Question

What is JSX?

Answer

A syntax extension for JavaScript that looks like HTML

Question

How do you embed expressions in JSX?

Answer

Use curly braces: {expression}

Question

What do you use instead of 'class' in JSX?

Answer

className

Question

What is a Fragment?

Answer

A way to return multiple elements without adding extra DOM nodes

Question

What is JSX?

Answer

JSX is a key concept in frontend development.

Revision Notes

Key Takeaways

  • 1.JSX compiles to React.createElement calls
  • 2.Use {} to embed JavaScript expressions
  • 3.Use className instead of class
  • 4.Use Fragments to return multiple elements
  • 5.JSX must have a single root element

Interview Tips

  • Explain what JSX is and how it compiles
  • Show knowledge of JSX gotchas
  • Demonstrate proper JSX syntax

Cheat Sheet

Cheat Sheet

Basic JSX

const el = <h1>Hello</h1>;
const el = <img src="url" alt="desc" />;

Expressions

const el = <h1>{name}</h1>;
const el = <p>{isLoggedIn ? "Hi" : "Bye"}</p>;

JSX Gotchas

  • className not class
  • htmlFor not for
  • Self-closing tags: <img />
  • Style as object: {{ color: "red" }}
  • Fragments: <>...</>

Event Handlers

<button onClick={handleClick}>Click</button>