Lesson 55 of 60 – React with Bootstrap
92%

React with Bootstrap

Bootstrap is a popular CSS framework that provides ready-made classes and components for creating responsive and attractive user interfaces. Bootstrap can be used with React to quickly build layouts, buttons, forms, cards, navigation bars, alerts, and other UI elements.

Note: Bootstrap provides styling and UI classes. React is responsible for building the component-based user interface and handling application logic.

1. What is Bootstrap?

Bootstrap is a front-end CSS framework containing reusable styles and components.

It helps developers create responsive websites without writing every CSS rule from scratch.

  • Responsive layouts
  • Buttons
  • Cards
  • Forms
  • Alerts
  • Navigation bars
  • Tables

2. Why Use Bootstrap with React?

React and Bootstrap can work together to create reusable and responsive interfaces.

Bootstrap can help you:

  • Create layouts quickly
  • Style React components
  • Build responsive pages
  • Create consistent UI elements
  • Reduce the amount of custom CSS

3. Installing Bootstrap

Bootstrap can be installed using npm.

npm install bootstrap

After installation, Bootstrap appears in the project's dependencies.

4. Importing Bootstrap CSS

After installing Bootstrap, import its CSS into your React entry file.

import "bootstrap/dist/css/bootstrap.min.css";

For example, this can be placed in main.jsx.

5. Importing Bootstrap JavaScript

Some Bootstrap interactive components require Bootstrap's JavaScript bundle.

import "bootstrap/dist/js/bootstrap.bundle.min.js";

The bundle includes Bootstrap's JavaScript functionality and its required Popper dependency.

6. Bootstrap Container

The container class provides a responsive fixed-width container.

function App() {

    return (
        <div className="container">

            <h1>
                React with Bootstrap
            </h1>

        </div>
    );
}

7. Bootstrap Container Fluid

The container-fluid class uses the full available width.

<div className="container-fluid">

    <h1>
        Full Width Layout
    </h1>

</div>

8. Bootstrap Grid System

Bootstrap provides a grid system based on rows and columns.

<div className="container">

    <div className="row">

        <div className="col-md-6">
            Left Column
        </div>

        <div className="col-md-6">
            Right Column
        </div>

    </div>

</div>

9. Bootstrap Columns

Bootstrap columns can be used to divide a row into responsive sections.

<div className="row">

    <div className="col-md-4">
        Column 1
    </div>

    <div className="col-md-4">
        Column 2
    </div>

    <div className="col-md-4">
        Column 3
    </div>

</div>

Three col-md-4 columns can occupy one complete Bootstrap row.

10. Bootstrap Buttons

Bootstrap provides predefined button classes.

<button className="btn btn-primary">
    Save
</button>

<button className="btn btn-success">
    Submit
</button>

<button className="btn btn-danger">
    Delete
</button>

11. Bootstrap Button Sizes

Bootstrap provides different button sizes.

<button className="btn btn-primary btn-sm">
    Small
</button>

<button className="btn btn-primary">
    Normal
</button>

<button className="btn btn-primary btn-lg">
    Large
</button>

12. Bootstrap Cards

Cards are useful for displaying grouped information.

<div className="card">

    <div className="card-body">

        <h5 className="card-title">
            React Course
        </h5>

        <p className="card-text">
            Learn React step by step.
        </p>

        <button className="btn btn-primary">
            Start
        </button>

    </div>

</div>

13. Bootstrap Alerts

Alerts can display messages to users.

<div className="alert alert-success">
    Registration successful!
</div>

<div className="alert alert-danger">
    Something went wrong.
</div>

14. Bootstrap Forms

Bootstrap provides classes for styling form controls.

<div className="mb-3">

    <label className="form-label">
        Email
    </label>

    <input
        type="email"
        className="form-control"
        placeholder="Enter email"
    />

</div>

15. React State with Bootstrap Forms

React state can control a Bootstrap-styled form input.

const [name, setName] =
    useState("");

return (
    <input
        type="text"
        className="form-control"
        value={name}
        onChange={e =>
            setName(
                e.target.value
            )
        }
    />
);

16. Bootstrap Navbar

Bootstrap provides classes for creating responsive navigation bars.

<nav className="navbar navbar-expand-lg bg-dark">

    <div className="container">

        <a
            className="navbar-brand text-white"
            href="#"
        >
            My App
        </a>

    </div>

</nav>

17. React Event with Bootstrap Button

Bootstrap buttons can use normal React event handlers.

function App() {

    function handleClick() {

        alert("Button clicked!");

    }

    return (
        <button
            className="btn btn-primary"
            onClick={handleClick}
        >

            Click Me

        </button>
    );
}

18. Bootstrap Spacing Classes

Bootstrap provides margin and padding utility classes.

<div className="mt-4">
    Content
</div>

<div className="p-3">
    Padded Content
</div>

<button className="mt-3 mb-3">
    Button
</button>

Classes such as mt, mb, ms, me, and p are useful for spacing.

19. Bootstrap Text Utilities

Bootstrap provides utility classes for text alignment and appearance.

<p className="text-center">
    Centered Text
</p>

<p className="text-primary">
    Primary Text
</p>

<p className="fw-bold">
    Bold Text
</p>

20. Bootstrap Responsive Classes

Bootstrap provides responsive classes that can change layout behavior at different screen sizes.

<div className="col-12 col-md-6 col-lg-4">

    Product

</div>

This allows the same component to adapt to different screen widths.

21. Conditional Bootstrap Classes

React expressions can dynamically select Bootstrap classes.

const [active, setActive] =
    useState(false);

return (
    <button
        className={
            active
                ? "btn btn-success"
                : "btn btn-secondary"
        }
        onClick={() =>
            setActive(!active)
        }
    >

        {active
            ? "Active"
            : "Inactive"}

    </button>
);

22. Bootstrap Table

Bootstrap table classes can be used to display data returned from React state or APIs.

<table className="table table-striped">

    <thead>
        <tr>
            <th>Name</th>
            <th>Course</th>
        </tr>
    </thead>

    <tbody>

        <tr>
            <td>Amit</td>
            <td>React</td>
        </tr>

    </tbody>

</table>

23. Rendering Bootstrap Cards with map()

React can generate Bootstrap cards from an array.

const courses = [
    "React",
    "JavaScript",
    "Python"
];

function Courses() {

    return (
        <div className="row">

            {courses.map(
                (course, index) => (

                <div
                    className="col-md-4"
                    key={index}
                >

                    <div className="card mb-3">

                        <div className="card-body">

                            <h5>
                                {course}
                            </h5>

                            <button
                                className="btn btn-primary"
                            >
                                Start
                            </button>

                        </div>

                    </div>

                </div>

            ))}

        </div>
    );
}

24. Bootstrap Modal in React

A modal can be displayed conditionally using React state.

const [show, setShow] =
    useState(false);

return (
    <>

        <button
            className="btn btn-primary"
            onClick={() =>
                setShow(true)
            }
        >

            Open Modal

        </button>

        {show && (

            <div className="modal d-block">

                <div className="modal-dialog">

                    <div className="modal-content">

                        <div className="modal-header">
                            <h5>
                                Hello
                            </h5>
                        </div>

                        <div className="modal-body">
                            Modal content
                        </div>

                        <div className="modal-footer">

                            <button
                                className="btn btn-secondary"
                                onClick={() =>
                                    setShow(false)
                                }
                            >
                                Close
                            </button>

                        </div>

                    </div>

                </div>

            </div>

        )}

    </>
);

For production applications, Bootstrap's modal behavior can also be integrated with Bootstrap's JavaScript or a React-specific component library.

25. Bootstrap Icons with React

Bootstrap Icons is a separate icon library that can be installed and used with React.

npm install bootstrap-icons

Then import the icon CSS:

import "bootstrap-icons/font/bootstrap-icons.css";

Example:

<i className="bi bi-house"></i>

<i className="bi bi-person"></i>

26. Bootstrap with React Components

Bootstrap classes can be placed directly inside reusable React components.

function CourseCard({
    title,
    description
}) {

    return (
        <div className="card mb-3">

            <div className="card-body">

                <h5 className="card-title">
                    {title}
                </h5>

                <p className="card-text">
                    {description}
                </p>

                <button
                    className="btn btn-primary"
                >
                    Learn More
                </button>

            </div>

        </div>
    );
}

27. Bootstrap vs React-Bootstrap

Bootstrap React-Bootstrap
Primarily uses Bootstrap CSS classes Provides React components for Bootstrap UI
Example: className="btn btn-primary" Example: <Button>
Can be used directly with React Designed specifically around React components

Both approaches can be used to build React interfaces. The choice depends on the project's requirements and preferred development style.

28. Complete Bootstrap React Example

import { useState } from "react";

import "bootstrap/dist/css/bootstrap.min.css";

function Course() {

    const [started, setStarted] =
        useState(false);

    return (
        <div className="container mt-5">

            <div className="row">

                <div className="col-md-8 mx-auto">

                    <div className="card shadow">

                        <div className="card-body">

                            <h1 className="card-title">
                                React Course
                            </h1>

                            <p className="card-text">
                                Learn React step by step.
                            </p>

                            {started && (
                                <div className="alert alert-success">
                                    Course Started!
                                </div>
                            )}

                            <button
                                className="btn btn-primary"
                                onClick={() =>
                                    setStarted(true)
                                }
                            >

                                Start Course

                            </button>

                        </div>

                    </div>

                </div>

            </div>

        </div>
    );
}

export default Course;

29. Common Bootstrap Mistakes in React

  • Using class instead of className.
  • Forgetting to install Bootstrap.
  • Forgetting to import Bootstrap CSS.
  • Writing invalid JSX nesting.
  • Using JavaScript expressions incorrectly inside JSX.
  • Forgetting key when rendering lists.
  • Mixing custom CSS and Bootstrap classes without a clear structure.
  • Using Bootstrap JavaScript components without configuring the required JavaScript bundle.

30. Bootstrap with React Best Practices

  • Install Bootstrap through npm for project-based applications.
  • Import Bootstrap CSS in the application entry point.
  • Use Bootstrap utility classes to reduce unnecessary custom CSS.
  • Create reusable React components for repeated UI elements.
  • Use Bootstrap's responsive grid for different screen sizes.
  • Use React state and events for interactive behavior.
  • Keep component structure organized.
  • Use meaningful class combinations.
  • Use custom CSS only when Bootstrap does not meet the design requirement.
  • Keep UI styling separate from application logic where practical.
React
  ↓
Components
  ↓
Bootstrap Classes
  ↓
Responsive UI

📌 Key Points

  • Bootstrap is a popular CSS framework for responsive interfaces.
  • Bootstrap can be installed with npm install bootstrap.
  • Bootstrap CSS can be imported into a React application.
  • Use className instead of HTML's class attribute.
  • Bootstrap provides containers, rows, columns, buttons, cards, forms, and alerts.
  • React state can control Bootstrap-styled interactive elements.
  • Bootstrap grid classes help create responsive layouts.
  • Bootstrap classes can be used inside reusable React components.
  • Bootstrap and React-Bootstrap are different approaches to using Bootstrap with React.
  • React handles application behavior while Bootstrap primarily handles UI styling.

🧠 Quick Quiz

Question: Which attribute should be used instead of class when applying Bootstrap classes in JSX?