Your First React Application
Build your first React application using components, JSX, props, CSS styling, and reusable UI elements.
1. Introduction
React is a JavaScript library used to build user interfaces. A React application is generally created by combining reusable components that describe different parts of the screen.
In this lesson, we will create a simple training institute application with a heading, course cards, and a footer. We will also learn how the application is rendered in the browser.
2. First React Application Flow
main.jsx
Starts the React application
App.jsx
Combines the application's components
Header
Institute name
CourseCard
Course information
Footer
Closing message
Browser UI
The rendered React application
3. Before You Start
React Project
A Vite React project should already be created.
Dependencies
Project dependencies should be installed with npm.
Code Editor
Use VS Code or another suitable code editor.
Development Server
Run the project with npm run dev.
4. Step 1: Understand the Project Files
We will mainly work with these files in the
src folder.
App.jsx and
App.css for the main example.
5. Step 2: Create the App Component
Open src/App.jsx. Replace its contents with
the following complete example.
import './App.css';
function Header() {
return (
<header className="app-header">
<h1>CIIT Training Institute</h1>
<p>Learn. Practice. Grow.</p>
</header>
);
}
function CourseCard({ title, description }) {
return (
<article className="course-card">
<h3>{title}</h3>
<p>{description}</p>
<button type="button">Explore Course</button>
</article>
);
}
function CourseList() {
return (
<section className="course-section">
<h2>Our Popular Courses</h2>
<div className="course-grid">
<CourseCard
title="Java Full Stack"
description="Learn Java, SQL, APIs, and web development."
/>
<CourseCard
title="Python Full Stack"
description="Learn Python, databases, and web development."
/>
<CourseCard
title="React Development"
description="Build modern user interfaces using React."
/>
</div>
</section>
);
}
function Footer() {
return (
<footer className="app-footer">
<p>Start learning today and build your future.</p>
</footer>
);
}
function App() {
return (
<div className="training-app">
<Header />
<main>
<CourseList />
</main>
<Footer />
</div>
);
}
export default App;
Header, CourseCard,
CourseList, and Footer are
components. The App component combines them
to create the complete page.
6. Step 3: Add CSS Styling
Open src/App.css and replace its contents
with the following CSS.
.training-app {
min-height: 100vh;
color: #1e293b;
background: #f8fafc;
font-family: Arial, sans-serif;
}
.app-header {
padding: 38px 20px;
text-align: center;
color: white;
background: linear-gradient(135deg, #4338ca, #2563eb);
}
.app-header h1 {
margin: 0 0 10px;
font-size: 2rem;
}
.app-header p {
margin: 0;
color: #e0e7ff;
}
.course-section {
max-width: 1100px;
margin: 0 auto;
padding: 35px 20px;
}
.course-section h2 {
margin-bottom: 24px;
text-align: center;
}
.course-grid {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 20px;
}
.course-card {
padding: 22px;
background: white;
border: 1px solid #e2e8f0;
border-radius: 12px;
box-shadow: 0 4px 12px rgba(15, 23, 42, 0.06);
}
.course-card h3 {
margin-top: 0;
color: #3730a3;
}
.course-card p {
line-height: 1.7;
color: #64748b;
}
.course-card button {
padding: 10px 14px;
color: white;
background: #4338ca;
border: 0;
border-radius: 7px;
cursor: pointer;
}
.app-footer {
padding: 20px;
text-align: center;
background: #e2e8f0;
}
.app-footer p {
margin: 0;
color: #334155;
}
@media (max-width: 767px) {
.course-grid {
grid-template-columns: 1fr;
}
.app-header h1 {
font-size: 1.6rem;
}
}
.cshtml page, CSS at-rules use
@media so Razor outputs a single
@media in the rendered stylesheet.
In your React file App.css, write normal
CSS as @media, with only one @.
7. Step 4: Understand the Component Structure
React allows us to separate the application into smaller components. In this example, the component structure looks like this:
App
Root component
Header
Page heading
CourseList
Course section
Footer
Bottom message
App
Combines the main components into one application.
Header
Displays the institute name and introductory text.
CourseList
Groups the course cards into a single section.
CourseCard
Displays individual course information through props.
Footer
Displays the closing message.
8. Step 5: Understand Props
Props are values passed from a parent component to a child component. We use props to display different course information without writing a separate component for every course.
function CourseCard({ title, description }) {
return (
<article>
<h3>{title}</h3>
<p>{description}</p>
</article>
);
}
Passing Different Props
<CourseCard
title="Java Full Stack"
description="Learn Java and web development."
/>
<CourseCard
title="React Development"
description="Learn to build React interfaces."
/>
Expected Results
Learn Java and web development.
Learn to build React interfaces.
9. Step 6: Run the Application
Save both App.jsx and App.css.
Open your terminal in the React project directory and run:
npm run dev
If the development server is already running, you can normally leave it running while you edit the files.
Save the Files
Save App.jsx and App.css.
Check the Terminal
Resolve compilation errors if any are displayed.
Open the Browser
Visit the local URL shown by Vite.
Check the Result
Verify the heading, course cards, buttons, and footer.
10. Expected Browser Output
After the code compiles successfully, the page should display a heading, three course cards, and a footer message.
CIIT Training Institute
Learn. Practice. Grow.
Our Popular Courses
Java Full Stack
Learn Java, SQL, APIs, and web development.
Python Full Stack
Learn Python, databases, and web development.
React Development
Build modern user interfaces using React.
This is a static illustration of the expected appearance. It is not a live React application or a preview of your running project.
11. React Concepts Used in This Application
| Concept | Where It Is Used | Purpose |
|---|---|---|
| Components | Header, CourseCard, CourseList, Footer | Breaks the UI into reusable parts. |
| JSX | Return statements | Describes the component's UI. |
| Props | CourseCard | Passes course titles and descriptions. |
| CSS | App.css | Controls colors, spacing, cards, and layout. |
| Composition | App uses Header, CourseList, and Footer | Combines components to form a larger UI. |
12. Common Errors and Solutions
| Problem | Possible Cause | Solution |
|---|---|---|
| CSS is not applied | CSS file is not imported or the class names differ. | Check import './App.css' and class names. |
| Blank browser page | Compilation error or incorrect component export. | Check the terminal and browser console. |
| Component is not displayed | The component is not used in the returned JSX. | Check the component tree inside App. |
| Props are undefined | A prop is missing or its name does not match. | Compare the prop names passed by the parent and read by the child. |
| JSX syntax error | Missing closing tags, parentheses, or braces. | Check the JSX structure and the terminal error. |
Debugging Tip
Read the terminal error first. React and Vite usually provide helpful details about syntax errors, missing imports, or invalid component code.
13. Practice Task
Task: Customize Your First React Application
- Replace the default content of
App.jsx. - Create Header, CourseCard, CourseList, and Footer components.
- Display at least three different courses.
- Pass each course title and description through props.
- Style the page using
App.css. - Make sure the layout works on smaller screens.
- Run the application using
npm run dev. - Change one course title and verify the updated browser output.
Summary
- A React application can be built using reusable components.
App.jsxcombines the main parts of the UI.- JSX describes the content returned by components.
- Props provide different information to reusable components.
App.csscontrols the application's appearance.npm run devstarts the Vite development server in a typical Vite project.- Terminal messages help identify compilation errors.