Reactstrap: React Bootstrap Components
Reactstrap offers a comprehensive collection of stateless React components designed to implement Bootstrap 5's user interface framework. The library, currently at stable version 9.2.3 (released September 2024), maintains a release cadence primarily focused on bug fixes and minor feature enhancements, with major versions typically aligning with Bootstrap's own releases. A key differentiator is its complete independence from jQuery and Bootstrap's own JavaScript, relying instead on React's declarative composition model for content rendering through `props.children`. For advanced positioning requirements in components like Tooltips, Popovers, and Dropdowns, Reactstrap integrates Poppers.js via `react-popper`, ensuring robust and accessible UI behavior. The package also ships with full TypeScript type definitions, enhancing developer experience for TypeScript projects.
Common errors
-
Module not found: Can't resolve 'bootstrap/dist/css/bootstrap.css' in '...' OR Cannot find module 'bootstrap/dist/css/bootstrap.css'
cause Bootstrap CSS is not installed or the import path is incorrect.fixRun `npm install bootstrap` to install the package. Ensure the import statement is `import 'bootstrap/dist/css/bootstrap.css';` and placed in a project entry file like `src/index.js`. -
TypeError: Cannot read properties of undefined (reading 'useState') OR hooks can only be called inside of the body of a function component
cause Using an outdated version of React that does not support Hooks (React 16.8.0+ is required by Reactstrap v9).fixEnsure your project's `react` and `react-dom` versions are `^16.8.0` or higher. Upgrade using `npm install react@latest react-dom@latest`. -
Reactstrap components render but have no styling applied.
cause Bootstrap's CSS file has not been correctly imported into the application.fixVerify that `import 'bootstrap/dist/css/bootstrap.css';` is present and correctly placed in your main application entry file (e.g., `src/index.js`). Also, confirm `bootstrap` is installed via `npm list bootstrap`.
Warnings
- breaking Reactstrap v9 is built exclusively for Bootstrap 5. Projects using Bootstrap 4 must use Reactstrap v8 or earlier.
- gotcha Reactstrap does not include Bootstrap's CSS. Users must explicitly install and import `bootstrap/dist/css/bootstrap.css` in their project's entry file to get correct styling.
- gotcha Some Reactstrap components expose an `innerRef` prop for direct DOM access, which differs from React's standard `ref` prop. This can be confusing when migrating components or using `forwardRef`.
Install
-
npm install reactstrap -
yarn add reactstrap -
pnpm add reactstrap
Imports
- Button
import Button from 'reactstrap';
import { Button } from 'reactstrap'; - Modal
const { Modal } = require('reactstrap');import { Modal, ModalHeader, ModalBody, ModalFooter } from 'reactstrap'; - Input
import * as Reactstrap from 'reactstrap'; <Reactstrap.Input />
import { Input } from 'reactstrap';
Quickstart
import React, { useState } from 'react';
import ReactDOM from 'react-dom/client';
import 'bootstrap/dist/css/bootstrap.css';
import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from 'reactstrap';
function App() {
const [modalOpen, setModalOpen] = useState(false);
const toggle = () => setModalOpen(!modalOpen);
return (
<div className="App p-4">
<h1>Welcome to Reactstrap!</h1>
<Button color="primary" onClick={toggle}>
Open Modal
</Button>
<Modal isOpen={modalOpen} toggle={toggle}>
<ModalHeader toggle={toggle}>Modal Title</ModalHeader>
<ModalBody>
This is a sample modal content. You can put any React components here.
</ModalBody>
<ModalFooter>
<Button color="secondary" onClick={toggle}>Cancel</Button>
<Button color="primary" onClick={toggle}>Do Something</Button>
</ModalFooter>
</Modal>
</div>
);
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);