React Components Basics
# React Components Basics
1. Introduction
At the very core of React is the concept of Components. If you understand components, you understand React. Instead of viewing a web page as a single massive document, React encourages you to view it as a collection of smaller, independent, and reusable building blocks. In this chapter, we will learn how to create, structure, and combine components to build a full UI.2. Learning Objectives
By the end of this chapter, you will be able to:- Define what a React component is.
- Understand the basic structure of a component.
- Import and export components between files.
- Understand component composition (nesting components).
- Create a modular UI architecture.
3. Beginner-Friendly Explanations
What are Components?
Think of a component as a custom HTML tag. Standard HTML gives you<button>, <h1>, and <div>. React allows you to create <MyCustomButton>, <UserProfile>, and <NavigationMenu>.
A component is essentially a JavaScript function that returns JSX (UI markup).
Why Use Components?
-
1.
Reusability: Write code once, use it everywhere. If you build a nice
<Button />component, you can use it 50 times across your app. If you need to change the button's color, you change it in *one* file, and all 50 buttons update instantly!
- 2. Organization: It's much easier to maintain 10 small files with 50 lines of code each than one massive file with 500 lines of code.
4. Component Structure and Syntax
Let's look at how to define a basic component.Rule 1: Capitalize Component Names
React components must start with a capital letter (e.g.,Header, not header). React treats lowercase tags as standard HTML elements and capitalized tags as custom components.
```jsx id="ch4_ex1" // Example React component // 1. Define the function with a Capital letter function Sidebar() { // 2. Return JSX return ( <aside className="sidebar"> <ul> <li>Home</li> <li>About</li> <li>Contact</li> </ul> </aside> ); }
export default Sidebar; // 3. Export it so other files can use it
jsx id="ch4_ex2" // Example React component - The small building blocks
function Header() { return <header><h1>My Awesome Website</h1></header>; }
function MainContent() { return ( <main> <h2>Welcome!</h2> <p>This is the main content area of the application.</p> </main> ); }
function Footer() { return <footer><p>© 2026 My Awesome Website</p></footer>; }
// Composing them in the parent 'App' component function App() { return ( <div className="app-container"> <Header /> <MainContent /> <Footer /> </div> ); }
html <!-- Example HTML Output --> <div class="app-container"> <header><h1>My Awesome Website</h1></header> <main> <h2>Welcome!</h2> <p>This is the main content area of the application.</p> </main> <footer><p>© 2026 My Awesome Website</p></footer> </div>
jsx id="ch4projheader" // Example React component export default function Header() { return ( <header className="bg-gray-800 text-white p-4 shadow-md flex justify-between items-center"> <div className="text-xl font-bold">TechBlog</div> <nav className="flex gap-4"> <a href="#" className="hover:text-blue-400">Articles</a> <a href="#" className="hover:text-blue-400">Tutorials</a> </nav> </header> ); }
jsx id="ch4projfooter" // Example React component export default function Footer() { return ( <footer className="bg-gray-900 text-gray-400 text-center p-6 mt-10"> <p>Built with React & Tailwind CSS.</p> </footer> ); }
jsx id="ch4projapp" // Example React component import Header from './Header'; import Footer from './Footer';
export default function App() { return ( <div className="min-h-screen flex flex-col bg-gray-50"> <Header /> {/* Main content takes up remaining space */} <main className="flex-grow p-8 max-w-4xl mx-auto w-full"> <h1 className="text-3xl font-bold mb-4">Latest Tech News</h1> <div className="bg-white p-6 rounded-lg shadow border"> <h2 className="text-xl font-semibold">React is awesome</h2> <p className="mt-2 text-gray-600">Component composition makes UI development a breeze.</p> </div> </main>
<Footer />
</div>
);
}
``
12. Coding Challenges
Challenge 1: Create an ArticleList component. Inside it, render three instances of an Article component.
13. MCQs with Answers
Q1: Why must React components start with a capital letter?
A) It is a JavaScript rule.
B) So React can distinguish them from standard HTML tags.
C) It improves performance.
D) It is required for CSS styling.
*Answer: B*
Q2: What is the term for placing smaller components inside larger ones?
A) Component inheritance
B) Component binding
C) Component composition
D) Component rendering
*Answer: C*
14. Interview Questions
-
Q: Explain the concept of Component Composition.
-
Q: What are the advantages of a component-based architecture?
15. FAQs
Can a component contain other components?
Yes! This is the entire point of React. The App component usually contains layout components, which contain page components, which contain UI components like buttons and forms. This creates a "Component Tree".
16. Summary
Components are JavaScript functions that return JSX. By breaking your UI down into modular, capitalized components and composing them together, you create clean, reusable, and easily maintainable web applications.
17. Next Chapter Recommendation
You now know how to make components, but currently, all our components display static data. What if we want to pass different text to our Header` component? In Chapter 5: Functional Components in React and Chapter 6: Props, we will unlock the true power of dynamic components.