Showing posts with label react. Show all posts
Showing posts with label react. Show all posts

React 5

 

Chapter 5 — Conditional Rendering

🎯 Goal

Learn how to show different UI based on different conditions.

This is one of the Top 5 most-used React skills in real projects.


First, one new idea (2 minutes)

Imagine you're entering a movie theater.

The security guard asks:

"Do you have a ticket?"

If Yes → Enter.

If No → Buy a ticket.

React does the same thing.

Instead of always showing the same UI, it decides what to show.

Condition
      │
      ▼
Is Logged In?
   │      │
 Yes      No
 │         │
Home     Login

New Syntax #1 — Ternary Operator

Instead of writing:

If condition is true
    show A
Else
    show B

React uses:

condition ? A : B

Think of it like:

Question ?

Yes Answer

:

No Answer

Example:

const isLoggedIn = true;

export default function Home() {
  return (
    <>
      {isLoggedIn ? <h1>Welcome</h1> : <h1>Please Login</h1>}
    </>
  );
}

New Syntax #2 — AND (&&)

Sometimes you only want to show something when a condition is true.

Example:

{isAdmin && <button>Delete User</button>}

Meaning:

If isAdmin is true

↓

Show button

Otherwise

↓

Show nothing

MCQs


Q1

Why do we use conditional rendering?

(A) To install packages

(B) To show different UI based on a condition

(C) To create folders

(D) To style components


Answer: (B)

Mini Explanation

Real apps don't always show the same screen.


Q2

Which syntax represents a ternary operator?

(A)

condition ? A : B

(B)

condition && A

(C)

condition || A

(D)

condition = A

Answer: (A)


Q3

Predict the output.

const isLoggedIn = true;

export default function Home() {
  return (
    <>
      {isLoggedIn ? <h1>Welcome</h1> : <h1>Login</h1>}
    </>
  );
}

(A) Welcome

(B) Login

(C) Both

(D) Error


Answer: (A)


Q4

Predict the output.

const isLoggedIn = false;

export default function Home() {
  return (
    <>
      {isLoggedIn ? <h1>Welcome</h1> : <h1>Login</h1>}
    </>
  );
}

(A) Welcome

(B) Login

(C) Both

(D) Error


Answer: (B)


Q5

Find the bug.

const isLoggedIn = true;

export default function Home() {
  return (
    <>
      {isLoggedIn ? <h1>Welcome</h1>}
    </>
  );
}

(A) No bug

(B) Missing : something

(C) Missing export

(D) Missing return


Answer: (B)

Mini Explanation

A ternary always needs both parts.

Correct:

isLoggedIn ? <h1>Welcome</h1> : <h1>Login</h1>

Q6

What does this display?

const isAdmin = true;

export default function Home() {
  return (
    <>
      {isAdmin && <button>Delete</button>}
    </>
  );
}

(A) Delete button

(B) Nothing

(C) Error

(D) undefined


Answer: (A)


Q7

Predict the output.

const isAdmin = false;

export default function Home() {
  return (
    <>
      {isAdmin && <button>Delete</button>}
    </>
  );
}

(A) Delete button

(B) Nothing

(C) Error

(D) undefined


Answer: (B)

Mini Explanation

&& only renders the right side when the left side is true.


Q8

Which code is better?

You only want to show a warning when isExpired is true.

(A)

isExpired
  ? <p>Expired</p>
  : null

(B)

isExpired && <p>Expired</p>

(C) Both work, but (B) is shorter and more common.

(D) Neither


Answer: (C)

Mini Explanation

Use && when there's nothing to show if the condition is false.


Q9

Predict the output.

const age = 20;

export default function Home() {
  return (
    <>
      {age >= 18 ? <h1>Adult</h1> : <h1>Child</h1>}
    </>
  );
}

(A) Adult

(B) Child

(C) Both

(D) Error


Answer: (A)


Q10

Find the bug.

const isAdmin = true;

export default function Home() {
  return (
    <>
      isAdmin && <button>Delete</button>
    </>
  );
}

(A) No bug

(B) Condition should be inside {}

(C) Missing export

(D) Missing button


Answer: (B)

Mini Explanation

JavaScript inside JSX must be wrapped in curly braces.

Correct:

{isAdmin && <button>Delete</button>}

Q11

Predict the output.

const stock = 0;

export default function Home() {
  return (
    <>
      {stock > 0
        ? <h2>In Stock</h2>
        : <h2>Out of Stock</h2>}
    </>
  );
}

(A) In Stock

(B) Out of Stock

(C) Error

(D) Both


Answer: (B)


Q12

Which statement is true?

(A) Ternary chooses between two UI options.

(B) && always shows both sides.

(C) JSX doesn't support conditions.

(D) React only supports if.


Answer: (A)


Q13

Predict the output.

const hasDiscount = true;

export default function Home() {
  return (
    <>
      <h1>Product</h1>
      {hasDiscount && <p>20% OFF</p>}
    </>
  );
}

(A) Product

(B) Product + 20% OFF

(C) 20% OFF only

(D) Error


Answer: (B)


Q14

Find the bug.

const isLoggedIn = false;

export default function Home() {
  return (
    <>
      {isLoggedIn ? <h1>Welcome</h1> <h1>Login</h1>}
    </>
  );
}

(A) No bug

(B) Missing :

(C) Missing return

(D) Missing export


Answer: (B)


Q15

Predict the output.

const score = 95;

export default function Home() {
  return (
    <>
      {score >= 50
        ? <h2>Pass</h2>
        : <h2>Fail</h2>}
    </>
  );
}

(A) Pass

(B) Fail

(C) Error

(D) Nothing


Answer: (A)


Q16

Interview Trap 🚨

Which code is cleaner when showing an "Admin Panel" only to admins?

(A)

isAdmin
  ? <AdminPanel />
  : null

(B)

isAdmin && <AdminPanel />

(C) Both work, but (B) is the pattern you'll see most often.

(D) Neither


Answer: (C)

Mini Explanation

When there's no "else" UI, && is simpler and easier to read.


Q17

Predict the output.

const premium = false;

export default function Home() {
  return (
    <>
      <h1>Dashboard</h1>
      {premium && <p>Premium Features</p>}
    </>
  );
}

(A) Dashboard

(B) Dashboard + Premium Features

(C) Premium Features

(D) Error


Answer: (A)


Q18 (Hard)

You're building an e-commerce website.

Which approach is best?

(A) Always display "Out of Stock"

(B)

product.inStock
  ? <button>Buy Now</button>
  : <button>Notify Me</button>

(C) Always display "Buy Now"

(D) Duplicate the page


Answer: (B)

Mini Explanation

Real apps change the UI based on data. If a product is available, users can buy it. Otherwise, they can ask to be notified.


⭐ Chapter Summary (The 20% You'll Use Every Day)

Remember these four patterns:

1. Show one of two options

condition ? A : B

Example:

isLoggedIn
  ? <HomePage />
  : <LoginPage />

2. Show something only when the condition is true

condition && A

Example:

isAdmin && <DeleteButton />

3. JavaScript inside JSX goes inside {}

{isLoggedIn ? <Home /> : <Login />}

4. Choose the right pattern

  • Two possible UIs → ? :

  • Show-or-hide UI → &&

This simple decision will help you write cleaner React code.


🎯 Mini Coding Challenge

Create these variables:

const isLoggedIn = true;
const isAdmin = false;

Requirements:

  • If the user is logged in, show:

Welcome Back

Otherwise:

Please Login
  • Show this button only if the user is an admin:

<button>Delete User</button>

Use both:

  • ? :

  • &&


🚀 Mini Real-World Project

Shopping Website

Create this data:

const products = [
  {
    name: "iPhone 16",
    price: "$999",
    inStock: true,
  },
  {
    name: "MacBook Air",
    price: "$1199",
    inStock: false,
  },
  {
    name: "AirPods Pro",
    price: "$249",
    inStock: true,
  },
];

Render the products using map().

For each product:

  • Show its name.

  • Show its price.

  • If inStock is true, display:

Buy Now

Otherwise display:

Out of Stock

This combines everything you've learned so far:

  • ✅ Components

  • ✅ Props

  • map()

  • ✅ Conditional Rendering


📈 Progress Check

You've now learned the core patterns that appear in almost every React/Next.js codebase:

  • ✅ Components

  • ✅ Props

  • ✅ Rendering Lists (map)

  • ✅ Conditional Rendering

These four concepts alone cover a large portion of everyday React UI work.

Next Chapter

When you reply "ok", we'll move to Chapter 6: Event Handling (onClick, onChange, onSubmit).

This is where your apps stop being static and start responding to user actions like button clicks, typing, and form submissions. It's a major step toward building interactive, real-world applications.

React 4

 

Chapter 4 — Rendering Lists with map()

🎯 Goal

Learn how to display many items using one component and one loop instead of writing the same code repeatedly.


First, one new idea (2 minutes)

Imagine you're writing the names of 100 students on certificates.

❌ Bad idea:

Rahul
Amit
Sara
Priya
...

(write each one manually)

✅ Better idea:

For every student,
print the student's name.

React works the same way.

Instead of writing:

<ProductCard name="iPhone" />
<ProductCard name="MacBook" />
<ProductCard name="AirPods" />

We usually store the data:

const products = [
  "iPhone",
  "MacBook",
  "AirPods"
];

Then generate the UI automatically.


New Syntax

const products = ["iPhone", "MacBook", "AirPods"];

products.map((product) => (
  <p>{product}</p>
));

What is map()?

Think of it like a photocopy machine.

Input:

Apple
Banana
Orange

Machine:

Wrap every fruit inside <p>

Output:

<p>Apple</p>
<p>Banana</p>
<p>Orange</p>

New Syntax Explained

products.map((product) => ...)
  • products → the array

  • .map() → visit every item

  • product → the current item

You can choose any name:

products.map((item) => ...)

or

products.map((fruit) => ...)

The name doesn't matter.


MCQs

Q1

Why do we use map() in React?

(A) To style components

(B) To display multiple items from an array

(C) To create folders

(D) To install packages


Answer: (B)

Mini Explanation

map() lets us generate UI for every item in an array.


Q2

Given:

const fruits = ["Apple", "Banana"];

Which code displays both fruits?

(A)

fruits.map((fruit) => <p>{fruit}</p>)

(B)

fruit.map((fruits) => <p>{fruit}</p>)

(C)

map(fruits)

(D)

<p>{fruits}</p>

Answer: (A)

Mini Explanation

Call map() on the array.


Q3

Predict the output.

const fruits = ["Apple", "Banana"];

export default function Home() {
  return (
    <>
      {fruits.map((fruit) => (
        <p>{fruit}</p>
      ))}
    </>
  );
}

(A) Apple Banana

(B) Apple

(C) Banana

(D) Error


Answer: (A)


Q4

How many <p> elements are created?

const colors = ["Red", "Blue", "Green"];
colors.map((color) => (
  <p>{color}</p>
));

(A) 1

(B) 2

(C) 3

(D) 4


Answer: (C)

Mini Explanation

One <p> is created for each item.


Q5

Predict the output.

const numbers = [10, 20];
numbers.map((number) => (
  <h1>{number}</h1>
));

(A) 10 20

(B) 1020

(C) Error

(D) Blank page


Answer: (A)


Q6

Find the bug.

const fruits = ["Apple", "Banana"];

export default function Home() {
  return (
    <>
      {fruit.map((fruit) => (
        <p>{fruit}</p>
      ))}
    </>
  );
}

(A) No bug

(B) Should use fruits.map(...)

(C) Missing return

(D) Missing export


Answer: (B)

Mini Explanation

The array is called fruits, not fruit.


Q7

Which variable represents the current item?

products.map((product) => ...)

(A) products

(B) map

(C) product

(D) None


Answer: (C)

Mini Explanation

product is the current item being processed.


Q8

Which code is better?

Option A

<ProductCard name="iPhone" />
<ProductCard name="MacBook" />
<ProductCard name="AirPods" />

Option B

products.map((product) => (
  <ProductCard name={product} />
))

(A) Option A

(B) Option B

(C) Both are equally good

(D) Neither


Answer: (B)

Mini Explanation

It scales easily. Add a new product to the array, and the UI updates automatically.


Q9

Predict the output.

const animals = ["Dog"];
animals.map((animal) => (
  <h2>{animal}</h2>
));

(A) Dog

(B) Dogs

(C) Error

(D) Nothing


Answer: (A)


Q10

Find the bug.

const fruits = ["Apple", "Banana"];

export default function Home() {
  return (
    <>
      {fruits.map((item) => (
        <p>{items}</p>
      ))}
    </>
  );
}

(A) No bug

(B) Should use {item}

(C) Missing export

(D) Missing map()


Answer: (B)

Mini Explanation

You named the variable item but tried to use items.

Names must match.


Q11

Predict the output.

const cities = ["Delhi", "Mumbai", "Pune"];
cities.map((city) => (
  <h3>{city}</h3>
));

(A) Delhi Mumbai Pune

(B) Delhi

(C) Pune

(D) Error


Answer: (A)


Q12

Which statement is true?

(A) map() only works with numbers.

(B) map() works with arrays.

(C) map() only works in Next.js.

(D) map() creates CSS.


Answer: (B)


Q13

Predict the output.

const names = [];
names.map((name) => (
  <p>{name}</p>
));

(A) Error

(B) Blank output

(C) undefined

(D) One empty paragraph


Answer: (B)

Mini Explanation

The array is empty, so there are no items to render.


Q14

Which is easier to maintain?

(A) Writing 100 <ProductCard /> components manually

(B) Storing products in an array and using map()

(C) Copying HTML repeatedly

(D) Creating one file per product


Answer: (B)


Q15

Predict the output.

const scores = [90, 80, 70];
scores.map((score) => (
  <p>{score}</p>
));

(A) 90 80 70

(B) 90

(C) Error

(D) Blank page


Answer: (A)


Q16

Interview Trap 🚨

Which code will produce a warning in React?

(A)

products.map((product) => (
  <p>{product}</p>
))

(B)

products.map((product) => (
  <p key={product}>{product}</p>
))

(C) Both

(D) Neither


Answer: (A)

Mini Explanation

When rendering a list, React expects a special prop called key.

Without it, you'll see a warning like:

"Each child in a list should have a unique 'key' prop."

For now, remember this rule:

Whenever you use map() to render a list, add a key.

Example:

products.map((product) => (
  <p key={product}>{product}</p>
));

We'll learn why React needs key in a later chapter.


Q17

Predict the output.

const books = ["Clean Code", "Deep Work"];
books.map((book) => (
  <h2>{book}</h2>
));

(A) Clean Code Deep Work

(B) Deep Work Clean Code

(C) Error

(D) One heading


Answer: (A)


Q18 (Hard)

You're building an e-commerce website with 5,000 products.

Which approach is better?

(A) Write 5,000 ProductCard components manually.

(B) Store the products in an array and use map().

(C) Create 5,000 pages with repeated HTML.

(D) Copy and paste each product.


Answer: (B)

Mini Explanation

This is exactly how real-world apps work. Product data comes from a database or API, and map() renders each item automatically.


⭐ Chapter Summary (The 20% You'll Use Every Day)

Remember these five patterns:

1. Store your data in an array.

const fruits = ["Apple", "Banana"];

2. Use map() to visit every item.

fruits.map((fruit) => ...)

3. Render JSX for each item.

fruits.map((fruit) => (
  <p>{fruit}</p>
));

4. Use {} to insert JavaScript values.

<p>{fruit}</p>

5. Add a key when rendering lists.

fruits.map((fruit) => (
  <p key={fruit}>{fruit}</p>
));

For now, just remember: List = map() + key.


🎯 Mini Coding Challenge

Create an array:

const skills = [
  "HTML",
  "CSS",
  "JavaScript",
  "React",
  "Next.js"
];

Display all the skills using map().

Bonus: Add a key to each rendered item.


🚀 Mini Real-World Project

Product Catalog

Create an array like this:

const products = [
  { name: "iPhone 16", price: "$999" },
  { name: "MacBook Air", price: "$1199" },
  { name: "AirPods Pro", price: "$249" }
];

Create a reusable ProductCard component that accepts:

  • name

  • price

Then use map() to render all the products.

Rule: Don't manually write three ProductCard components. Generate them from the array.


💡 One Important Note

This chapter introduced arrays, map(), and a small preview of key because these are among the most frequently used patterns in React.

In the next chapter, we'll combine what you've learned with Conditional Rendering—showing or hiding UI based on conditions (for example, "Show 'Out of Stock' only when a product isn't available"). This pattern appears in almost every production Next.js application.

Reply "ok" when you're ready for Chapter 5: Conditional Rendering.

React 3

 

Chapter 3 — Props (Making Components Dynamic)

🎯 Goal

Learn how to send data from one component to another so you can reuse the same component with different content.


First, one new idea (2 minutes)

Imagine a coffee shop.

There is one coffee machine (the component).

Customers place different orders:

  • Coffee → Latte

  • Coffee → Cappuccino

  • Coffee → Espresso

The machine is the same. Only the order changes.

Props work exactly like that.

Without props:

<Button />
<Button />
<Button />

All three buttons look exactly the same.

With props:

<Button text="Save" />
<Button text="Delete" />
<Button text="Cancel" />

Same component.
Different data.


New Syntax

Passing a prop

<Button text="Save" />

Here,

  • text → prop name

  • "Save" → value


Receiving a prop

function Button(props) {
  return <button>{props.text}</button>;
}

New Syntax Explained

You'll see curly braces {} inside JSX.

<button>{props.text}</button>

Think of it like saying:

"Insert the value of props.text here."

If props.text is "Save":

<button>Save</button>

MCQs

Q1

What is the main purpose of props?

(A) To style components

(B) To send data from one component to another

(C) To install packages

(D) To create folders


Answer: (B)

Mini Explanation

Props allow a parent component to send data to a child component.


Q2

Which code passes a prop?

(A)

<Button />

(B)

<Button text="Save" />

(C)

Button("Save")

(D)

<Button = "Save" />

Answer: (B)

Mini Explanation

Props are written like HTML attributes.


Q3

What will this display?

function Button(props) {
  return <button>{props.text}</button>;
}

export default function Home() {
  return <Button text="Save" />;
}

(A) Button

(B) Save

(C) props.text

(D) Error


Answer: (B)

Mini Explanation

props.text contains "Save".


Q4

Predict the output.

function Button(props) {
  return <button>{props.text}</button>;
}

export default function Home() {
  return (
    <>
      <Button text="Save" />
      <Button text="Delete" />
    </>
  );
}

(A) Save Delete

(B) Save Save

(C) Delete Delete

(D) Error


Answer: (A)

Mini Explanation

Each component receives its own prop value.


Q5

Find the bug.

function Button(props) {
  return <button>{props.title}</button>;
}

export default function Home() {
  return <Button text="Save" />;
}

(A) No bug

(B) Should use props.text

(C) Missing export

(D) Missing return


Answer: (B)

Mini Explanation

You passed text but tried to read title.

The names must match.


Q6

Which code is correct?

(A)

<Button text="Login" />

(B)

<Button("Login") />

(C)

<Button text=Login />

(D)

<Button:Login />

Answer: (A)

Mini Explanation

String values go inside quotes.


Q7

What will appear?

function Card(props) {
  return <h2>{props.title}</h2>;
}

export default function Home() {
  return <Card title="React" />;
}

(A) React

(B) title

(C) Card

(D) Error


Answer: (A)


Q8

Find the bug.

function Card(props) {
  return <h2>{props.name}</h2>;
}

export default function Home() {
  return <Card title="Next.js" />;
}

(A) Should use props.title

(B) Missing button

(C) Missing export

(D) No bug


Answer: (A)

Mini Explanation

The prop name is title, not name.


Q9

Predict the output.

function Welcome(props) {
  return <h1>Hello {props.name}</h1>;
}

export default function Home() {
  return <Welcome name="Pramod" />;
}

(A) Hello

(B) Hello Pramod

(C) Pramod

(D) Error


Answer: (B)


Q10

What happens here?

function Welcome(props) {
  return <h1>Hello {props.name}</h1>;
}

export default function Home() {
  return <Welcome />;
}

(A) Hello

(B) Hello undefined

(C) Error

(D) Blank page


Answer: (B)

Mini Explanation

No name prop was passed.

So props.name is undefined.


Q11

Which code is better?

Option A

<h2>Apple</h2>
<h2>Banana</h2>
<h2>Mango</h2>

Option B

<Fruit name="Apple" />
<Fruit name="Banana" />
<Fruit name="Mango" />

(A) Option A

(B) Option B

(C) Both

(D) Neither


Answer: (B)

Mini Explanation

Reusable components reduce repeated code.


Q12

Predict the output.

function Price(props) {
  return <p>${props.value}</p>;
}

export default function Home() {
  return (
    <>
      <Price value="100" />
      <Price value="250" />
    </>
  );
}

(A) $100 $250

(B) $100 $100

(C) $250 $250

(D) Error


Answer: (A)


Q13

Which statement is true?

(A) Props make components reusable.

(B) Props can only contain text.

(C) Props work only in Next.js pages.

(D) Props are used for CSS.


Answer: (A)

Mini Explanation

Props let one component display different data.


Q14

Find the bug.

function Button(props) {
  return <button>{props.text}</button>;
}

export default function Home() {
  return <Button Text="Save" />;
}

(A) No bug

(B) Should pass text, not Text

(C) Missing return

(D) Missing export


Answer: (B)

Mini Explanation

Prop names are case-sensitive.

text and Text are different.


Q15

Predict the output.

function Book(props) {
  return <h2>{props.title}</h2>;
}

export default function Home() {
  return (
    <>
      <Book title="Atomic Habits" />
      <Book title="Deep Work" />
      <Book title="Clean Code" />
    </>
  );
}

(A) One book

(B) Three book titles

(C) Error

(D) Blank page


Answer: (B)


Q16

Which is more reusable?

(A)

function Button() {
  return <button>Save</button>;
}

(B)

function Button(props) {
  return <button>{props.text}</button>;
}

(C) Both are equally reusable.

(D) Neither


Answer: (B)

Mini Explanation

The second component can display Save, Delete, Cancel, or anything else.


Q17

What will this display?

function User(props) {
  return <h2>{props.name}</h2>;
}

export default function Home() {
  return (
    <>
      <User name="Amit" />
      <User name="Sara" />
    </>
  );
}

(A) Amit Sara

(B) Sara Amit

(C) Amit Amit

(D) Error


Answer: (A)


Q18 (Hard)

You're building an online store.

Which approach is better?

(A) Create a different ProductCard component for every product.

(B) Create one ProductCard component and pass different props like name, price, and image.

(C) Copy and paste the HTML for every product.

(D) Create one page per product with repeated code.


Answer: (B)

Mini Explanation

Real-world apps reuse the same component with different props. This keeps your code clean and easy to update.


⭐ Chapter Summary (The 20% You'll Use Every Day)

Remember these four patterns:

1. Pass data

<Button text="Save" />

2. Receive data

function Button(props) {
  return <button>{props.text}</button>;
}

3. Use {} to display JavaScript values

<h1>{props.name}</h1>

4. Prop names must match exactly

✅ Correct:

<Button text="Save" />
props.text

❌ Wrong:

<Button Text="Save" />
props.text

🎯 Mini Coding Challenge

Create a reusable ProductCard component.

Requirements:

  • Accept these props:

    • name

    • price

Example:

<ProductCard name="iPhone 16" price="$999" />
<ProductCard name="MacBook Air" price="$1199" />
<ProductCard name="AirPods Pro" price="$249" />

Expected output:

iPhone 16
$999

MacBook Air
$1199

AirPods Pro
$249

🚀 Mini Real-World Project

Restaurant Menu

Build a simple restaurant menu using one reusable component.

Create a MenuItem component that accepts:

  • name

  • price

Use it like this:

<MenuItem name="Veg Burger" price="$5" />
<MenuItem name="Pizza" price="$12" />
<MenuItem name="Pasta" price="$10" />
<MenuItem name="Cold Coffee" price="$4" />

Rule: Don't create four different components. Build one reusable component and pass different props.


💡 Tiny Improvement for Future Chapters

From the next chapter onward, I'll also include "Interview Trap" questions—common mistakes that even junior developers make in real React/Next.js interviews (for example, confusing props with state, incorrect JSX syntax, or subtle rendering bugs). These are the kinds of questions that help you become job-ready, not just tutorial-ready.

Reply "ok" when you've finished this chapter, and we'll move on to Chapter 4: Rendering Lists with map(), where you'll learn one of the most frequently used patterns in production Next.js applications.

React 2

 

Chapter 2 — Components & JSX

🎯 Goal

Learn how to build reusable UI pieces (components) using JSX—the foundation of every Next.js app.


First, one new idea (2 minutes)

Imagine you're building a LEGO house.

Instead of building every window from scratch, you create one window and reuse it.

A component is exactly that—a reusable piece of UI.

Example:

function Button() {
  return <button>Save</button>;
}

Whenever you write:

<Button />

React displays:

<button>Save</button>

New Syntax

You'll see this syntax a lot:

<Button />

This is called using a component.

Notice:

  • HTML tags → lowercase (<button>, <div>)

  • Components → Capitalized (<Button />, <Navbar />)

That's one of the most common beginner mistakes.


MCQs

Q1

Which of these is a valid React component name?

(A) button
(B) navbar
(C) Navbar
(D) header-component

Answer: (C)

Mini Explanation:
React components should start with a capital letter.


Q2

Which syntax is used to render a component?

(A) <Navbar></Navbar> only

(B) <Navbar />

(C) Navbar()

(D) {Navbar}

Answer: (B)

Mini Explanation:
<Navbar /> is the standard JSX syntax. (<Navbar></Navbar> also works, but when there are no children, the self-closing form is preferred.)


Q3

Which tag is treated as an HTML element?

(A) <Button />

(B) <Navbar />

(C) <button>

(D) <Card />

Answer: (C)

Mini Explanation:
Lowercase tags are HTML.
Capitalized tags are React components.


Q4

What will this display?

export default function Home() {
  return <h1>Welcome</h1>;
}

(A) Welcome

(B) Error

(C) Blank page

(D) undefined

Answer: (A)

Mini Explanation:
The component returns an <h1>, so the page shows Welcome.


Q5

Which file usually contains reusable UI?

(A) public

(B) components

(C) node_modules

(D) styles

Answer: (B)

Mini Explanation:
Most Next.js projects keep reusable components inside the components folder.


Q6

What happens here?

function Button() {
  return <button>Save</button>;
}

export default function Home() {
  return <Button />;
}

(A) Save button appears

(B) Error

(C) Nothing

(D) undefined

Answer: (A)

Mini Explanation:
Home renders the Button component.


Q7

Find the bug.

function button() {
  return <button>Save</button>;
}

export default function Home() {
  return <button />;
}

(A) No bug

(B) Component name should start with a capital letter

(C) Button cannot return HTML

(D) Missing semicolon

Answer: (B)

Mini Explanation:
It should be:

function Button() {
  return <button>Save</button>;
}

Q8

Which code is better?

Option A

export default function Home() {
  return (
    <>
      <button>Save</button>
      <button>Cancel</button>
      <button>Delete</button>
    </>
  );
}

Option B

function Button() {
  return <button>Save</button>;
}

export default function Home() {
  return <Button />;
}

(A) Option A

(B) Option B

(C) Both are always equally good

(D) Neither

Answer: (B)

Mini Explanation:
Reusable components make your code easier to maintain.


Q9

Predict the output.

function Hello() {
  return <h2>Hello</h2>;
}

export default function Home() {
  return (
    <>
      <Hello />
      <Hello />
    </>
  );
}

(A) Hello

(B) Hello Hello

(C) Error

(D) Blank page

Answer: (B)

Mini Explanation:
Each <Hello /> renders once.


Q10

Find the bug.

function Navbar() {
  return <h1>Logo</h1>;
}

export default function Home() {
  return <navbar />;
}

(A) No bug

(B) Should be <Navbar />

(C) Should return <div>

(D) Missing export

Answer: (B)

Mini Explanation:
Lowercase names are treated as HTML tags, not components.


Q11

What will appear?

function Card() {
  return <p>React</p>;
}

export default function Home() {
  return (
    <>
      <h1>Course</h1>
      <Card />
    </>
  );
}

(A) Course

(B) React

(C) Course then React

(D) Error

Answer: (C)

Mini Explanation:
Both elements are rendered in order.


Q12

Find the bug.

function Card() {
}

(A) Missing return statement

(B) Missing import

(C) Missing export

(D) No bug

Answer: (A)

Mini Explanation:
Without return, nothing is rendered.


Q13

Predict the output.

function Welcome() {
  return <h2>Hi</h2>;
}

export default function Home() {
  return (
    <>
      <Welcome />
      <h1>Everyone</h1>
    </>
  );
}

(A) Hi Everyone

(B) Everyone Hi

(C) Error

(D) Only Hi

Answer: (A)

Mini Explanation:
JSX renders from top to bottom.


Q14

Which is the better file structure?

(A)

app/
  page.js
  Navbar.js
  Button.js
  Card.js

(B)

app/
  page.js

components/
  Navbar.js
  Button.js
  Card.js

(C) Both are equally preferred

(D) Put everything in public

Answer: (B)

Mini Explanation:
Keeping reusable components in a separate components folder keeps projects organized.


Q15

How many times is Button rendered?

function Button() {
  return <button>Buy</button>;
}

export default function Home() {
  return (
    <>
      <Button />
      <Button />
      <Button />
    </>
  );
}

(A) 1

(B) 2

(C) 3

(D) 0

Answer: (C)

Mini Explanation:
Every <Button /> creates one button.


Q16

Which statement is true?

(A) Components help reuse UI.

(B) Every page should copy and paste its HTML.

(C) Components can only be used once.

(D) Components only work with buttons.

Answer: (A)

Mini Explanation:
Reusability is one of the biggest advantages of React.


Q17

Predict the output.

function Title() {
  return <h1>Shop</h1>;
}

export default function Home() {
  return (
    <>
      <Title />
      <Title />
      <Title />
    </>
  );
}

(A) Shop

(B) Shop Shop

(C) Shop Shop Shop

(D) Error

Answer: (C)

Mini Explanation:
The component is rendered three times.


Q18 (Hard)

Which approach is better for a website with 100 pages using the same navigation bar?

(A) Copy the navbar into all 100 pages.

(B) Create one Navbar component and reuse it.

(C) Write the navbar inside public.

(D) Create 100 different navbars.

Answer: (B)

Mini Explanation:
One reusable component means one place to update. If you change the logo or add a menu item, every page gets the update automatically.


⭐ Chapter Summary (The 20% You'll Use Every Day)

Remember these four patterns:

  1. Capitalize component names.

function Navbar() {}
  1. Use components like HTML tags.

<Navbar />
  1. Keep reusable UI in components/.

  2. If you repeat UI, make it a component instead of copying and pasting.

If you remember just these four ideas, you'll understand a huge part of how React and Next.js apps are built.


🎯 Mini Coding Challenge

Create a project with this structure:

app/
  page.js

components/
  Header.js
  Footer.js

Requirements:

  • Header should display My Blog.

  • Footer should display © 2026 My Blog.

  • Render both components inside app/page.js.

Bonus: Add another component called Hero that displays:

Learn Next.js
Build Real Projects

🚀 Mini Real-World Project

Personal Portfolio Homepage

Build this page using only components.

Home Page
│
├── Header
├── Hero
├── Skills
├── Projects
└── Footer

Each section should be its own component stored in the components folder.

Rule: Do not copy HTML between sections. If something can become a reusable component, make it one.


Next Chapter Preview

When you reply "ok", we'll cover Props—the feature that lets components become dynamic instead of always displaying the same content. This is one of the biggest "aha!" moments in React and is used constantly in real-world Next.js projects.

React 1

Excellent. Based on your goal (building real projects → remote job), I would not teach Next.js the way most tutorials do.

Most courses spend hours on routing theory, rendering history, or obscure APIs. Instead, we'll learn the 20% of concepts you'll use in 80% of real projects.

Next.js Roadmap (Job-Oriented)

Phase 1 — JavaScript You'll Actually Need (Quick Revision)

Goal: Remove JS gaps that cause problems in Next.js.

  1. Modern ES6+

  2. Modules (import / export)

  3. Array methods (map, filter, find)

  4. Destructuring

  5. Spread operator

  6. Async / Await

  7. Fetch API

  8. Objects & Arrays

  9. Optional Chaining

  10. Template Literals


Phase 2 — React Essentials

Goal: Think in React.

  1. JSX

  2. Components

  3. Props

  4. Rendering Lists

  5. Conditional Rendering

  6. Events

  7. State (useState)

  8. Forms

  9. Effects (useEffect)

  10. Component Communication


Phase 3 — Next.js Core (Most Important)

This is where we'll spend most of our time.

  1. Creating a Next.js project

  2. App Router

  3. Pages

  4. Layouts

  5. Navigation

  6. Link

  7. Images

  8. Static Assets

  9. CSS Modules

  10. Tailwind CSS

  11. Server Components

  12. Client Components

  13. Fetching Data

  14. Loading UI

  15. Error UI

  16. Dynamic Routes

  17. Route Groups


Phase 4 — Real Backend Skills

  1. API Routes

  2. Route Handlers

  3. GET

  4. POST

  5. PUT

  6. DELETE

  7. Forms

  8. Validation

  9. Environment Variables


Phase 5 — Database

  1. Prisma

  2. PostgreSQL

  3. CRUD

  4. Relationships

  5. Migrations


Phase 6 — Authentication

  1. Login

  2. Register

  3. Sessions

  4. Protected Routes

  5. Middleware


Phase 7 — Production Skills

  1. File Upload

  2. Search

  3. Pagination

  4. Sorting

  5. Filtering

  6. Caching

  7. SEO

  8. Metadata

  9. Deployment (Vercel)

  10. Performance


Phase 8 — Portfolio Projects

We'll build progressively:

  • Notes App

  • Blog

  • Expense Tracker

  • Authentication System

  • Dashboard

  • Mini Ecommerce

  • Full Stack SaaS Clone


Learning Format

Every chapter will contain:

  • 🎯 Goal

  • 15–25 MCQs

  • Answers immediately after each question

  • Bug finding

  • Predict output

  • Better code choices

  • Mini challenge

  • Mini project


Chapter 1 — Modern Project Structure & Creating Your First Next.js App

🎯 Goal

Understand what a Next.js project looks like and identify the files you'll touch every day.

Assumption for this chapter: You already know basic HTML, CSS, and enough JavaScript to read simple examples. We'll fill any missing JS as we encounter it.


Q1 (Easy)

Which command creates a new Next.js project?

A)

npm create next-app

B)

npx create-next-app@latest

C)

next new app

D)

npm next install

✅ Answer

B

Why?

npx create-next-app@latest downloads and runs the latest project generator without installing it globally.


Q2

After creating the project, which command starts the development server?

A)

npm build

B)

npm start

C)

npm run dev

D)

next serve

✅ Answer

C

Why?

During development, you'll use:

npm run dev

Q3

Which folder contains your application pages when using the App Router?

A)

src

B)

public

C)

app

D)

styles

✅ Answer

C

Everything users visit begins inside the app folder.


Q4

You want an image like logo.png to be available at /logo.png in the browser.

Where should you place it?

A)

app

B)

public

C)

node_modules

D)

styles

✅ Answer

B

The public folder is for static files like images, PDFs, icons, and videos.


Q5

Which folder should you almost never edit manually?

A)

public

B)

node_modules

C)

app

D)

components

✅ Answer

B

It contains installed packages. Editing it is like changing the engine parts of a rented car—you'll lose those changes on reinstall.


Q6

What happens when you save a file while npm run dev is running?

A)

Restart your computer

B)

Nothing

C)

The browser usually updates automatically

D)

The project is deleted

✅ Answer

C

Next.js automatically reloads or refreshes the page.


Q7

Which file represents the homepage (/)?

A)

app/home/page.js

B)

app/index.js

C)

app/page.js

D)

pages/index.js

✅ Answer

C

app/page.js maps to the root URL /.


Q8

Suppose your project contains:

app
 ├── about
 │     page.js

Which URL opens it?

A)

/about

B)

/page

C)

/about/page

D)

/home

✅ Answer

A

Each folder becomes part of the URL, and page.js is the page for that folder.


Q9 — Predict the Output

app/page.js

export default function Home() {
  return <h1>Hello</h1>;
}

What appears?

A)

Nothing

B)

Hello

C)

Error

D)

undefined

✅ Answer

B

React renders the <h1> element.


Q10 — Find the Bug

function Home() {
    return <h1>Hello</h1>
}

What is missing?

A)

Nothing

B)

export default

C)

import React

D)

Semicolon

✅ Answer

B

A page component must be exported as the default export so Next.js can use it.

export default function Home() {
  return <h1>Hello</h1>;
}

New syntax: export default means "this is the main thing this file provides."


Q11

Which file is used to create a shared layout?

A)

layout.js

B)

template.js

C)

main.js

D)

wrapper.js

✅ Answer

A

layout.js wraps multiple pages so shared UI (like a navbar) doesn't have to be repeated.


Q12

Which folder usually stores reusable UI like buttons and cards?

A)

components

B)

public

C)

node_modules

D)

cache

✅ Answer

A

While optional, components is the common place for reusable pieces.


Q13 — Which Code Is Better?

Option A

function Home() {
  return <h1>Home</h1>;
}

export default Home;

Option B

export default function Home() {
  return <h1>Home</h1>;
}

✅ Answer

B

It's shorter and the style you'll see most often in real Next.js projects.


Q14 — Find the Bug

Project:

app
   about.js

Why doesn't /about work?

A)

Need CSS

B)

Should be:

app
   about
      page.js

C)

Need index.js

D)

Need main.js

✅ Answer

B

With the App Router, routes are folders containing a page.js file.


Q15 (Hardest)

Which structure creates both / and /products?

A)

app
   page.js
   products
      page.js

B)

pages
   index.js
   products.js

C)

src
   home.js

D)

routes
   products.js

✅ Answer

A

app/page.js serves /, and app/products/page.js serves /products.


🧠 Pattern to Remember

Think of the app folder like a building:

  • Every folder is a room (a URL segment).

  • Every page.js is the main activity in that room.

  • The layout.js is the hallway that connects rooms and stays the same as you move around.

Once you see this pattern, most routing in Next.js feels much simpler.


Mini Coding Challenge

Create this structure:

app
    page.js
    about
        page.js
    contact
        page.js

Display:

  • /Home

  • /aboutAbout Us

  • /contactContact

Try it yourself before checking the browser.


Mini Real-World Project

Company Website Skeleton

Using only today's concepts, create the basic structure for a small company website:

app
├── page.js          // Home
├── about
│   └── page.js
├── services
│   └── page.js
├── contact
│   └── page.js
├── layout.js
components
└── Navbar.js
public
└── logo.png

Your goals:

  • Create four pages.

  • Add a simple shared layout.

  • Create a reusable Navbar component (it can just display the company name for now).

  • Place a logo.png file in public so it's available later.


How we'll continue

When you reply with "ok", we'll move to Chapter 2: Components & JSX in Next.js, where you'll learn the most fundamental building block of every React/Next.js application through progressively harder MCQs, output prediction, bug-finding, and a mini project.