Skip to content
Tutorials Center
My learning

Topics

Lesson 50 of 58 ยท Quality

Using Bootstrap with React and Vue

Use Bootstrap 5 in React and Vue: plain CSS classes, who controls the DOM, react-bootstrap and bootstrap-vue-next, and when to avoid its JavaScript.

React and Vue build the page from components and own the DOM: when state changes, the framework decides which elements to create, update or remove. Bootstrap's JavaScript plugins work the other way around: they find elements by data-bs-* attributes, attach behavior and change classes and attributes directly. Two systems fighting over the same elements is the main thing to understand when you combine Bootstrap with a framework.

The good news is that Bootstrap's CSS works with any framework, because it is only class names. The decisions are about the JavaScript. This lesson explains three ways to use Bootstrap in React and Vue apps: plain CSS classes with your own state, the official-style component libraries (react-bootstrap and bootstrap-vue-next), and wrapping Bootstrap's plugins yourself. It also shows what to avoid.

The library code on this page (react-bootstrap, bootstrap-vue-next, bootstrap imports) needs npm and a build step, so those snippets are read-only. The two live examples use only Bootstrap's CSS from the CDN, with the interactive behavior written in React and Vue themselves.

The core idea: let the framework own the DOM

Compare how a modal opens in each world:

Approach Who opens the modal? Who owns the markup?
Plain Bootstrap Bootstrap JS, from data-bs-toggle="modal" Static HTML, plugin mutates it
React or Vue + Bootstrap CSS Your state (show is true or false) Framework renders or hides it
react-bootstrap / bootstrap-vue-next Component props or v-model Framework, with Bootstrap's styles

A plugin that adds classes (show, collapsing) and attributes to elements your framework also renders can cause subtle bugs: the framework re-renders and removes the class, a modal appears but its backdrop stays, or the plugin holds a reference to an element that no longer exists. Prefer state-driven markup.

Option 1: Bootstrap CSS plus your own components

Install Bootstrap and import its CSS once, at the top of your app's entry file:

Terminal
npm install bootstrap@5.3.3
JavaScript
// main.js (React or Vue)
import "bootstrap/dist/css/bootstrap.min.css";

(Or import your customized Sass, as in Bootstrap with Vite, Webpack and npm.)

After that, you write ordinary components with Bootstrap classes. In React the HTML attribute class becomes className; in Vue you keep class. Interactive pieces (open and closed states, selected tabs, validation messages) become state.

A React example

Here is a card list with a state-driven alert and a modal-like dialog that React controls completely: no Bootstrap JavaScript is involved. The component adds Bootstrap's stylesheet to the page for the demo (in a real app you would import the CSS as shown above).

React with Bootstrap classes

Everything is plain React: the dialog exists only while pending has a value, and React adds and removes the markup. We reuse Bootstrap's modal styles (modal, d-block, modal-backdrop show) but none of its JavaScript. This approach gives you full control, but note what you must provide yourself that Bootstrap's plugin gave for free: closing on Escape, trapping focus inside the dialog, restoring focus to the opener, and preventing the page behind from scrolling. Those are real accessibility requirements (Accessibility in Bootstrap), which is the strongest argument for using a library.

A Vue example

The same pattern in Vue, with a tabbed panel driven by a ref and a counter badge. It imports Vue's full browser build so the template can be written in the page.

Vue with Bootstrap classes

The tabs use Bootstrap's nav-tabs and active classes, but the choice of panel is a Vue ref. Because Vue renders only the active panel with v-if, there is no hidden DOM for Bootstrap to manage. Real tab widgets should also support arrow-key navigation and aria-controls; Bootstrap's plugin does that, and so do the libraries below.

Option 2: component libraries

Libraries rebuild Bootstrap's components as native framework components, with Bootstrap's CSS underneath and no jQuery and no dependency on Bootstrap's own JavaScript. They handle ARIA attributes, keyboard support and focus management for you.

React: react-bootstrap

Terminal
npm install react-bootstrap bootstrap
JSX
import "bootstrap/dist/css/bootstrap.min.css";
import { useState } from "react";
import Button from "react-bootstrap/Button";
import Modal from "react-bootstrap/Modal";
import Alert from "react-bootstrap/Alert";
import { Container, Row, Col } from "react-bootstrap";

export default function App() {
  const [show, setShow] = useState(false);

  return (
    <Container className="py-4">
      <Row>
        <Col md={6}>
          <Alert variant="info">Hello from react-bootstrap.</Alert>
          <Button variant="primary" onClick={() => setShow(true)}>Open dialog</Button>
        </Col>
      </Row>

      <Modal show={show} onHide={() => setShow(false)} centered>
        <Modal.Header closeButton>
          <Modal.Title>Confirm</Modal.Title>
        </Modal.Header>
        <Modal.Body>Focus is trapped and Escape closes this dialog.</Modal.Body>
        <Modal.Footer>
          <Button variant="secondary" onClick={() => setShow(false)}>Close</Button>
        </Modal.Footer>
      </Modal>
    </Container>
  );
}

Points to know:

  • Import from the subpath (react-bootstrap/Button) to keep bundles small, as shown above. Importing the whole package works too in modern bundlers.
  • Props map to Bootstrap concepts: variant="danger", size="lg", md={6} for columns, show and onHide for modals. Utilities remain plain className.
  • It follows a Bootstrap version: check the library's compatibility table before upgrading Bootstrap.

Vue: bootstrap-vue-next

The original BootstrapVue targeted Vue 2 and Bootstrap 4, and is no longer the right choice for new projects. For Vue 3 and Bootstrap 5 use bootstrap-vue-next:

Terminal
npm install bootstrap bootstrap-vue-next
JavaScript
// main.js
import { createApp } from "vue";
import { createBootstrap } from "bootstrap-vue-next";
import "bootstrap/dist/css/bootstrap.min.css";
import "bootstrap-vue-next/dist/bootstrap-vue-next.css";
import App from "./App.vue";

createApp(App).use(createBootstrap()).mount("#app");
VUE
<script setup>
import { ref } from "vue";
const show = ref(false);
</script>

<template>
  <BContainer class="py-4">
    <BAlert :model-value="true" variant="info">Hello from bootstrap-vue-next.</BAlert>
    <BButton variant="primary" @click="show = true">Open dialog</BButton>

    <BModal v-model="show" title="Confirm" centered>
      Focus is trapped and Escape closes this dialog.
    </BModal>
  </BContainer>
</template>

Components register globally with createBootstrap() (or can be imported individually for smaller bundles), v-model controls open state and the usual Bootstrap utility classes still work on every component. The project is younger and moves quickly, so pin a version and read its changelog.

Other options exist (Reactstrap for React, @popperjs/core based wrappers, and so on). Whichever library you choose, check that it is maintained, supports your Bootstrap and framework versions and has accessibility support for dialogs, menus and tabs.

Option 3: using Bootstrap's own JavaScript in a framework

Sometimes a library lacks a component, or you only need one plugin (for example a tooltip). You can use the real plugin, but give it a ref and manage its lifetime explicitly. Do not rely on data-bs-* auto-initialization, because the framework may add elements after Bootstrap has scanned the page.

JSX
// React: a tooltip created and disposed with the component
import { useEffect, useRef } from "react";
import { Tooltip } from "bootstrap";

export function HintButton({ text, children }) {
  const ref = useRef(null);

  useEffect(() => {
    const tooltip = new Tooltip(ref.current, { title: text });
    return () => tooltip.dispose();   // clean up when unmounted
  }, [text]);

  return <button ref={ref} className="btn btn-outline-secondary">{children}</button>;
}
VUE
<!-- Vue: same idea with onMounted and onBeforeUnmount -->
<script setup>
import { ref, onMounted, onBeforeUnmount } from "vue";
import { Tooltip } from "bootstrap";

const props = defineProps({ text: String });
const button = ref(null);
let tooltip;

onMounted(() => { tooltip = new Tooltip(button.value, { title: props.text }); });
onBeforeUnmount(() => tooltip?.dispose());
</script>

<template>
  <button ref="button" class="btn btn-outline-secondary"><slot /></button>
</template>

The rules:

  1. Create the plugin instance after the element exists (useEffect, onMounted).
  2. Dispose it when the component goes away, or you leak listeners and orphaned popups.
  3. Do not put data-bs-toggle on elements the framework re-renders constantly.
  4. Import only what you use (import { Tooltip } from "bootstrap"), as in Bootstrap with Vite, Webpack and npm.
  5. Let the framework control content (the title), and let the plugin control behavior.

Choosing an approach

Situation Best choice
A mostly static site with a few widgets Bootstrap CSS plus a few ref-managed plugins
An app with many interactive components (modals, dropdowns, tabs) react-bootstrap or bootstrap-vue-next
A design system with strict custom behavior Bootstrap CSS or Sass tokens and your own accessible components
Server-rendered pages with light JavaScript The regular CDN or bundle, with data-bs-* attributes

Also remember that Bootstrap's grid and utilities are still the biggest time saver, and they need no library at all.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why can Bootstrap's data-bs-* plugins cause problems in React or Vue?

    Bootstrap's plugins mutate classes and attributes directly, while frameworks re-render from state, so they can conflict.
  2. 2. What is the React equivalent of the HTML class attribute used in Bootstrap markup?

    JSX uses className because class is a reserved word in JavaScript.
  3. 3. What do you get from react-bootstrap or bootstrap-vue-next that a hand-written state-driven modal lacks?

    The libraries implement the accessibility behavior of Bootstrap's plugins as native framework components.
  4. 4. When you use a Bootstrap plugin directly in a component, what must you do on unmount?

    Disposing removes listeners and popups that the plugin created, which avoids leaks and orphaned elements.
  5. 5. Which library targets Vue 3 and Bootstrap 5?

    The original BootstrapVue targets Vue 2 and Bootstrap 4. bootstrap-vue-next is the Vue 3 and Bootstrap 5 successor.

Summary

  • Bootstrap's CSS works in any framework; the issue is its JavaScript, which manipulates the DOM that React and Vue also own.
  • The simplest approach is Bootstrap classes plus framework state (className in React, class in Vue), but you must implement focus management and keyboard support yourself.
  • react-bootstrap and bootstrap-vue-next rebuild components as native framework components with accessibility included; check version compatibility.
  • When using a plugin directly, create it after mount through a ref, dispose it on unmount and import only what you need.
  • Pick the approach by how interactive the app is, and keep the grid and utilities, which need no library.

Next lesson: Project: Landing Page, where you apply everything in a complete page.