Bootstrap in a React application, you can install it via npm and then import it into your project.
1. Create a React Project (if you haven’t already)
npx create-react-app my-app
cd my-app
2. Install Bootstrap
Install Bootstrap via npm:
npm install bootstrap
3. Import Bootstrap CSS
You need to import the Bootstrap CSS file into your project. You can do this in the src/index.js
file:
import 'bootstrap/dist/css/bootstrap.min.css';
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import './index.css';
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
4. Use Bootstrap Components
Now you can use Bootstrap classes in your React components. Here’s an example of using Bootstrap classes in a component:
import React from 'react';
export default function BootstrapExample() {
return (
<div className="container mt-5">
<h1 className="text-center">Hello, Bootstrap!</h1>
<button className="btn btn-primary">Click Me</button>
</div>
);
}
5. Optionally, Use React Bootstrap
If you prefer to use React components that wrap Bootstrap styles, you can install React Bootstrap:
npm install react-bootstrap
Then, import and use the React Bootstrap components:
import React from 'react';
import { Button, Container } from 'react-bootstrap';
import 'bootstrap/dist/css/bootstrap.min.css';
export default function ReactBootstrapExample() {
return (
<Container className="mt-5">
<h1 className="text-center">Hello, React Bootstrap!</h1>
<Button variant="primary">Click Me</Button>
</Container>
);
}
Bootstrap in Project Structure
Here is an example of what your project structure might look like after setting up Bootstrap:
my-app/
├── node_modules/
├── public/
├── src/
│ ├── App.js
│ ├── BootstrapExample.js
│ ├── ReactBootstrapExample.js
│ ├── index.css
│ └── index.js
├── .gitignore
├── package.json
├── README.md
└── yarn.lock