Menu

React Essentials for Next.js

Lesson 2: React Essentials for Next.js

What do you need to know from React?

Next.js is built on React, so every page is made of React components. You do not need to be a React expert, but you should recognise these ideas.

Concept

What it means

Example

ComponentA function that returns UIfunction CourseCard() { ... }
JSXHTML-like syntax inside JavaScript<h2>{title}</h2>
PropsInputs passed into a component<CourseCard title="SQL" />
StateData that changes and re-renders the UIconst [count, setCount] = useState(0)
Event handlerA function that runs on user actiononClick={() => setCount(count + 1)}
HookA function starting with use that adds React featuresuseState, useEffect
List renderingTurning an array into elementscourses.map(c => <li key={c.id}>...)
Conditional renderingShowing UI only when a condition is true{isFree && <span>Free</span>}

Example

type Course = { id: string; title: string; price: number };
function CourseCard({ course }: { course: Course }) {
return (
<article>
<h2>{course.title}</h2>
<p>{course.price === 0 ? 'Free' : `₹${course.price}`}</p>
</article>
);
}
export default function CourseList({ courses }: { courses: Course[] }) {
return (
<section>
{courses.map((course) => (
<CourseCard key={course.id} course={course} />
))}
</section>
);
}

How it works

  • CourseCard receives one course through props and displays its title and price.
  • CourseList loops through an array with map() and renders one card per course.
  • key helps React track each item when the list changes. Use a stable ID, not the array index.
  • The type Course line is TypeScript. It describes the shape of the data and catches mistakes early.

State and events (used only in interactive components, covered in Module 3):

'use client';

import { useState } from 'react';

export function LikeButton() {

const [likes, setLikes] = useState(0);

return <button onClick={() => setLikes(likes + 1)}>♥ {likes}</button>;

}

The 'use client' line is a Next.js signal that this component runs in the browser. You will learn exactly why in Module 3.

Common mistakes

Mistake

Example

Fix

Missing key in listscourses.map(c => <li>{c.title}</li>)Add key={c.id}
Using class in JSX<div class="card">Use className
Changing state directlylikes = likes + 1Call setLikes(likes + 1)
Lowercase component namesfunction courseCard()Components must start with a capital letter

Key takeaways

  • Components, props, and JSX are used in every Next.js file.
  • State, events, and most hooks only work in Client Components.
  • TypeScript types make props safer and are the default in new Next.js projects.

Try it: Write a CourseCard component that shows a title, level, and a "New" badge only when a isNew prop is true.


Nextjs Tutorial

J

Jebasta





Get in Touch For Details