React-Bootstrap Components
React-Bootstrap is a complete re-implementation of the Bootstrap 5 components using React, removing the need for jQuery. It provides a set of accessible and customizable React components that mirror Bootstrap's visual style and functionality. The current stable version is 2.10.10, with active development on a new major version (v3.0.0-beta.x) that introduces further breaking changes and improvements like enhanced ESM support. This library is a popular choice for developers who want to leverage Bootstrap's extensive styling framework within a React application without the performance overhead or potential conflicts of integrating traditional Bootstrap's JavaScript. Releases are frequent for both maintenance and beta branches, ensuring timely bug fixes and new features.
Common errors
-
TypeError: (0 , react_bootstrap_Button__WEBPACK_IMPORTED_MODULE_2__.default) is not a function
cause Incorrectly using named import for a default export from a sub-path, or attempting to use a CommonJS `require` call with a modern React setup.fixEnsure you are using `import Button from 'react-bootstrap/Button';` for default exports from sub-paths, and `import { NamedExport } from 'react-bootstrap';` for named exports directly from the main package if applicable. Avoid CommonJS `require` for components. -
Module not found: Error: Can't resolve 'bootstrap/dist/css/bootstrap.min.css'
cause The Bootstrap CSS file is not found, likely because `bootstrap` is not installed or the import path is incorrect.fixInstall Bootstrap as a dependency: `npm install bootstrap` or `yarn add bootstrap`, and ensure the import path `import 'bootstrap/dist/css/bootstrap.min.css';` is correct in your main application file.
Warnings
- breaking React-Bootstrap v3.0.0-beta.1 introduces breaking changes, including migration to the `clsx` library for class name management. Existing custom class logic might need adjustments.
- breaking The `Offcanvas` component underwent a significant change in its rendering behavior in v3.0.0-beta.1. This may affect how it integrates into existing layouts or state management.
- gotcha React-Bootstrap components require the Bootstrap CSS stylesheet to be included in your project for proper styling. Without it, components will appear unstyled.
- gotcha When upgrading React-Bootstrap, especially between minor versions of v2, ensure your React and React-DOM peer dependencies match the specified ranges to avoid type conflicts or runtime errors, particularly with React 19.
Install
-
npm install react-bootstrap -
yarn add react-bootstrap -
pnpm add react-bootstrap
Imports
- Button
import { Button } from 'react-bootstrap';import Button from 'react-bootstrap/Button';
- Modal
const Modal = require('react-bootstrap').Modal;import Modal from 'react-bootstrap/Modal';
- Navbar
import Navbar from 'react-bootstrap/Navbar';
- Form
import Form from 'react-bootstrap/Form';
Quickstart
import React, { useState } from 'react';
import Button from 'react-bootstrap/Button';
import Modal from 'react-bootstrap/Modal';
import Form from 'react-bootstrap/Form';
import Container from 'react-bootstrap/Container';
function MyFormModal() {
const [show, setShow] = useState(false);
const handleClose = () => setShow(false);
const handleShow = () => setShow(true);
const handleSubmit = (event: React.FormEvent) => {
event.preventDefault();
console.log('Form submitted!');
handleClose();
};
return (
<Container className="p-4">
<Button variant="primary" onClick={handleShow}>
Launch demo modal
</Button>
<Modal show={show} onHide={handleClose}>
<Modal.Header closeButton>
<Modal.Title>Login</Modal.Title>
</Modal.Header>
<Modal.Body>
<Form onSubmit={handleSubmit}>
<Form.Group className="mb-3" controlId="formBasicEmail">
<Form.Label>Email address</Form.Label>
<Form.Control type="email" placeholder="Enter email" />
<Form.Text className="text-muted">
We'll never share your email with anyone else.
</Form.Text>
</Form.Group>
<Form.Group className="mb-3" controlId="formBasicPassword">
<Form.Label>Password</Form.Label>
<Form.Control type="password" placeholder="Password" />
</Form.Group>
<Button variant="secondary" onClick={handleClose} className="me-2">
Close
</Button>
<Button variant="primary" type="submit">
Submit
</Button>
</Form>
</Modal.Body>
</Modal>
</Container>
);
}
export default MyFormModal;