Skip to content
beginnerPhase 36 · React

Components

Create functional and class components with proper structure and composition.

45m
0 problems
Topic Progress0%

Functional Components

Functional Components

Functional components are JavaScript functions that return JSX.

Basic Component

function Greeting() {
  return <h1>Hello, World!</h1>;
}

// Arrow function
const Greeting = () => {
  return <h1>Hello, World!</h1>;
};

// Implicit return
const Greeting = () => <h1>Hello, World!</h1>;

Component with Props

function Greeting({ name, age }) {
  return (
    <div>
      <h1>Hello, {name}!</h1>
      <p>You are {age} years old.</p>
    </div>
  );
}

// Usage
<Greeting name="Alice" age={30} />

Component Rules

  1. Name components with PascalCase
  2. Export components for reuse
  3. One component per file
  4. Return JSX or null
// Good
function UserProfile({ user }) {
  return (
    <div>
      <h2>{user.name}</h2>
      <p>{user.email}</p>
    </div>
  );
}

export default UserProfile;

Component File Structure

Component File Structure

Single File Component

// Button.jsx
import React from "react";

function Button({ label, onClick }) {
  return (
    <button onClick={onClick} className="btn">
      {label}
    </button>
  );
}

export default Button;

Component with Styles

// Button.jsx
import React from "react";
import "./Button.css";

function Button({ label, onClick, variant = "primary" }) {
  return (
    <button 
      onClick={onClick} 
      className={`btn btn-${variant}`}
    >
      {label}
    </button>
  );
}

export default Button;

Folder Structure

src/
  components/
    Button/
      Button.jsx
      Button.css
      Button.test.jsx
      index.js
    Card/
      Card.jsx
      Card.css
    Header/
      Header.jsx
      Header.css

Index Files

// components/Button/index.js
export { default } from "./Button";

// Usage
import Button from "./components/Button";

Component Composition

Component Composition

Compose components together to build complex UIs.

Basic Composition

function App() {
  return (
    <div className="app">
      <Header />
      <main>
        <Sidebar />
        <Content />
      </main>
      <Footer />
    </div>
  );
}

Passing Components as Props

function Card({ title, children }) {
  return (
    <div className="card">
      <h2>{title}</h2>
      <div className="card-body">{children}</div>
    </div>
  );
}

function App() {
  return (
    <Card title="My Card">
      <p>This is card content</p>
      <Button label="Click me" />
    </Card>
  );
}

Layout Components

function Layout({ children }) {
  return (
    <div className="layout">
      <header>Logo</header>
      <main>{children}</main>
      <footer>© 2024</footer>
    </div>
  );
}

// Usage
<Layout>
  <HomePage />
</Layout>

Composition vs Inheritance

React favors composition over inheritance:

// Good: Composition
function Dialog({ title, children }) {
  return (
    <div className="dialog">
      <h2>{title}</h2>
      {children}
    </div>
  );
}

function ConfirmDialog({ message, onConfirm }) {
  return (
    <Dialog title="Confirm">
      <p>{message}</p>
      <button onClick={onConfirm}>Yes</button>
    </Dialog>
  );
}

Practice Problems

0/3solved
Build Components Component

Create a reusable React component implementing Components. Include proper state management and accessibility.

Solution
// Production-ready component with:
// - Proper TypeScript types
// - Accessibility (ARIA)
// - Error boundaries
// - Loading states
// - Memoization where needed
Components Testing

Write unit and integration tests for Components using React Testing Library.

Solution
// Test coverage:
// 1. Rendering tests
// 2. Interaction tests
// 3. Edge case tests
// 4. Accessibility tests
Components Performance

Optimize Components for performance. Consider memoization, code splitting, and bundle size.

Solution
// Optimization techniques:
// 1. React.memo / useMemo / useCallback
// 2. Code splitting with lazy()
// 3. Virtual scrolling for lists
// 4. Image lazy loading
// 5. Bundle analysis

Quiz

1. What is the naming convention for React components?

Question 1 options

2. What must a functional component return?

Question 2 options

3. What is component composition?

Question 3 options

4. What is the children prop used for?

Question 4 options

Flashcards

Question

What is a functional component?

Answer

A JavaScript function that returns JSX

Question

How do you name React components?

Answer

With PascalCase

Question

What is the children prop?

Answer

A special prop that contains nested components

Question

What is component composition?

Answer

Building complex UIs by combining smaller components

Question

What is Components?

Answer

Components is a key concept in frontend development.

Revision Notes

Key Takeaways

  • 1.Functional components are functions that return JSX
  • 2.Use PascalCase for component names
  • 3.Components can be composed together
  • 4.The children prop enables nesting
  • 5.One component per file is best practice

Interview Tips

  • Show how to create a basic functional component
  • Explain component composition
  • Demonstrate how to use the children prop

Cheat Sheet

Cheat Sheet

Functional Component

function Greeting({ name }) {
  return <h1>Hello, {name}!</h1>;
}

Component Rules

  • PascalCase naming
  • Return JSX or null
  • One component per file
  • Export for reuse

Composition

function App() {
  return (
    <Layout>
      <Header />
      <Content />
      <Footer />
    </Layout>
  );
}

Children Prop

function Card({ title, children }) {
  return <div><h2>{title}</h2>{children}</div>;
}