Skip to main content
PProfileMatch

React Introduction: Components, JSX and Your First App

Web DevelopmentLesson 1 of 65 min read

What you'll learn

  • Explain what React is and the problem it solves compared with plain JavaScript DOM code
  • Write valid JSX, including expressions, attributes and the rules that trip up beginners
  • Create a new React project with Vite or Next.js and run it locally
  • Build and render your first function component

On this page

  1. What React is and why people use it
  2. JSX: HTML-like syntax inside JavaScript
  3. Create a project: Vite or Next.js
  4. Your first component, composed into an app
  5. Common mistakes
  6. How this shows up in interviews and real projects

Imagine a notification bell in the corner of a page. When a new message arrives, the red badge must change from 2 to 3, the dropdown list must grow, and the page title should show the count. In plain JavaScript you would hunt for each of those DOM nodes and update them by hand, and sooner or later one of them gets forgotten. React exists to remove that whole class of bug.

What React is and why people use it

React is a JavaScript library for building user interfaces out of small, reusable pieces called components. A component is a function that returns a description of what should be on screen. You describe the UI for the current data, and React works out which DOM nodes need to change when that data changes.

This is called declarative programming. With plain DOM code you write imperative steps: find the element, set its text, add a class, remove a child. With React you write "given this data, the UI looks like this", and the library handles the steps. The trade-off is that you learn a few concepts (components, props, state) and add a build step, but in return big interfaces stay predictable as they grow.

React itself is only the view layer. Routing, data fetching and server rendering come from the surrounding ecosystem. Frameworks such as Next.js bundle those choices for you, while Vite gives you a lightweight starting point where you add them yourself. Both are good places to learn.

JSX: HTML-like syntax inside JavaScript

Components return JSX, a syntax that looks like HTML but is really JavaScript. A build tool converts each tag into a function call that creates a description of an element. You never need to read that output, but it explains why JSX has stricter rules than HTML.

  1. A component must return a single root element. Wrap siblings in a <div> or in an empty fragment <>...</> if you do not want an extra node in the DOM.
  2. Every tag must be closed. Write <img src="a.png" alt="" /> and <br />, never <img> on its own.
  3. Use className instead of class, htmlFor instead of for, and camelCase for other attributes such as tabIndex and onClick.
  4. Curly braces {} embed a JavaScript expression: a variable, a function call, a ternary, items.length. Statements such as if and for do not fit inside braces.
  5. The style attribute takes an object, not a string: style={{ color: "teal", fontSize: 18 }}. The outer braces mean "JavaScript here" and the inner pair is the object literal.

Here is a profile card that uses variables, an expression and a method call inside JSX.

ProfileCard.tsx
function ProfileCard() {
  const name = "Asha Verma";
  const role = "Frontend Developer";
  const skills = ["React", "TypeScript", "CSS"];

  return (
    <section className="card">
      <h2>{name}</h2>
      <p>{role}</p>
      <p>Skills: {skills.join(", ")}</p>
      <p>Years of experience: {2 + 3}</p>
    </section>
  );
}

export default ProfileCard;

Output

Asha Verma            (heading)
Frontend Developer
Skills: React, TypeScript, CSS
Years of experience: 5

Create a project: Vite or Next.js

You need Node.js installed. The quickest route to a running app is Vite with the React and TypeScript template. If you plan to build a full website with routing and server rendering, Next.js is a sensible alternative.

Terminal
# Option 1: Vite (client-side React, very fast dev server)
npm create vite@latest my-app -- --template react-ts
cd my-app
npm install
npm run dev

# Option 2: Next.js (routing and server rendering built in)
npx create-next-app@latest my-next-app
cd my-next-app
npm run dev

Output

Local:   http://localhost:5173/    (Vite)
Local:   http://localhost:3000/    (Next.js)

In a Vite project the entry point is src/main.tsx. It finds the <div id="root"> in index.html and tells React to render your top-level component inside it. You rarely touch this file again, but it is worth reading once.

src/main.tsx
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import App from "./App";

createRoot(document.getElementById("root")!).render(
  <StrictMode>
    <App />
  </StrictMode>
);

StrictMode is a development-only helper. It runs some code twice on purpose to expose bugs such as missing cleanup in effects. It does nothing in production builds, so do not be surprised if a console.log prints twice while developing.

Your first component, composed into an app

Open src/App.tsx, delete the starter content and replace it with a small app that uses the ProfileCard above. Components can be used like HTML tags, and they can contain other components, which is how large UIs are built from small parts.

src/App.tsx
import ProfileCard from "./ProfileCard";

export default function App() {
  return (
    <main>
      <h1>Team directory</h1>
      <ProfileCard />
      <ProfileCard />
    </main>
  );
}

Output

Team directory        (page heading)
Asha Verma            (card 1)
Frontend Developer
Skills: React, TypeScript, CSS
Years of experience: 5
Asha Verma            (card 2, identical for now)
...

Both cards look identical because the component has its data hard-coded. The next lesson, React Components and Props, fixes that by letting the parent pass different data into each card.

Common mistakes

  • Returning two sibling elements. Symptom: the error "JSX expressions must have one parent element". Wrap them in a fragment <>...</>.
  • Using `class` instead of `className`. Symptom: styles do not apply and the console warns about an invalid DOM property.
  • Lowercase component names. Symptom: <profileCard /> renders nothing useful because React looks for an HTML tag with that name.
  • Putting a statement in braces. Writing {if (isAdmin) ...} fails to compile. Use a ternary {isAdmin ? <Admin /> : null} or compute the value before the return.
  • Calling a component like a function and defining one inside another. Define components at the top level of the file. A component declared inside another component is recreated on every render and loses its state.

How this shows up in interviews and real projects

Beginner interviews usually ask what JSX is, why the virtual DOM exists, and what the difference is between a component and an element. A solid answer is that JSX is syntax sugar for creating element descriptions, and that React compares the new description with the previous one to apply the smallest set of DOM changes. In real projects, almost everything you build is a tree of components like the one above, so fluency here pays off in every later lesson.

A small React project with a clear README is also a strong item for a resume. See how to write a resume that passes ATS for how to present projects, and browse open frontend jobs to see which React skills employers actually list.

Try it yourself

Create a JobCard component that shows a job title, a company name, a location and a salary range stored in variables inside the component. Render three of them inside an App component under a heading that says "Open roles".

Show a hint

Declare the variables above the return, embed them with {}, and remember to export the component and import it in App.tsx. Use a fragment if you need to return siblings.

Key takeaways

  • React builds UIs from components: functions that return JSX describing what should be on screen.
  • JSX is JavaScript in disguise, so it needs one root element, closed tags, className and expressions inside braces.
  • Component names must start with a capital letter and be defined at the top level of a file.
  • Vite is the lightest way to start a React app; Next.js adds routing and server rendering.

Test yourself

A trick from this lesson's ride: Declarative is ordering from a menu; imperative is cooking the meal yourself. Four more tricks like it wait along the route, each followed by a question.

Ride this lesson

Frequently asked questions

What is React and what is it used for?

React is a JavaScript library for building user interfaces from reusable components. It is used for interactive web apps such as dashboards, social feeds and e-commerce sites, and its sibling React Native applies the same ideas to mobile apps.

Do I need to learn JavaScript before React?

Yes, at least the basics. You should be comfortable with functions, arrow functions, arrays and their methods like map and filter, objects, destructuring and import/export before starting. React is mostly JavaScript, so gaps there show up as confusion later.

Should I use Vite or Next.js to learn React?

Either works. Vite gives a minimal client-side setup that makes the core ideas easy to see, while Next.js adds routing and server rendering that matter for production sites. Many learners start with Vite and move to Next.js once they are comfortable with components and state.