AskHandle Blog
Bootstrap for React

Bootstrap for React
When it comes to front-end development, React has carved out a coveted spot for itself. Its component-based architecture simplifies building interactive UIs, while its declarative nature makes code easier to understand. Pairing React with a robust CSS framework like Bootstrap creates a powerful combination that enhances both your development process and user experience. Bootstrap, with its pre-built components and responsive grid system, provides a seamless way to style your applications without reinventing the wheel.
Getting Started
Integrating Bootstrap in a React project is straightforward. You can start a React project using Create React App, a popular boilerplate for React applications. Here’s how to do it:
1npx create-react-app my-bootstrap-app
2cd my-bootstrap-appOnce your React app is set up, styling it with Bootstrap can be done in a few simple steps.
Installing Bootstrap
To use Bootstrap with React, you need to install Bootstrap and its dependencies. You can use npm or yarn for this.
1npm install bootstrap@5Or if you're using yarn:
1yarn add bootstrap@5After the installation, you need to import Bootstrap CSS into your src/index.js file.
1import 'bootstrap/dist/css/bootstrap.min.css';
2// Your other imports...
3
4const root = ReactDOM.createRoot(document.getElementById('root'));
5root.render(<App />);Now that Bootstrap is installed and imported, you can start using its classes and components.
Using Bootstrap Classes
Bootstrap classes can be used directly in your JSX, courtesy of the className attribute.
1import React from 'react';
2
3function App() {
4 return (
5 <div className="container">
6 <header className="d-flex justify-content-center py-3">
7 <h1 className="text-primary">Welcome to My Bootstrap-React App!</h1>
8 </header>
9 <div className="row">
10 <div className="col-md-6 shadow p-3 mb-5 bg-white rounded">
11 <h2>Left Column</h2>
12 <p>This is some text within a left column.</p>
13 </div>
14 <div className="col-md-6 shadow p-3 mb-5 bg-white rounded">
15 <h2>Right Column</h2>
16 <p>This is some text within a right column.</p>
17 </div>
18 </div>
19 </div>
20 );
21}
22
23export default App;In this example, Bootstrap's container, row, col, d-flex, justify-content-center, and text-primary classes are used to organize and style the elements.
React-Bootstrap Components
While using Bootstrap classes directly in JSX is an option, integrating React-Bootstrap components gives you a more React-idiomatic way to build your UI. React-Bootstrap provides Bootstrap components as React components.
Installing React-Bootstrap
You need to install React-Bootstrap and its peer dependencies.
1npm install react-bootstrap@next bootstrap@5Or with yarn:
1yarn add react-bootstrap@next bootstrap@5Using React-Bootstrap Components
Once installed, you can use components like Button, Card, Form, and others directly in your React components.
1import React from 'react';
2import { Container, Row, Col, Card, Button } from 'react-bootstrap';
3
4function App() {
5 return (
6 <Container>
7 <Row className="justify-content-md-center">
8 <Col md="auto">
9 <Card style={{ width: '18rem' }}>
10 <Card.Body>
11 <Card.Title>Card Title</Card.Title>
12 <Card.Text>
13 Some quick example text to build on the card title and make up the bulk of the card's content.
14 </Card.Text>
15 <Button variant="primary">Go somewhere</Button>
16 </Card.Body>
17 </Card>
18 </Col>
19 </Row>
20 </Container>
21 );
22}
23
24export default App;React-Bootstrap components are built with React's declarative syntax in mind, making them intuitive and easy to use. A simple, reusable pattern can be employed to create complex layouts without excessive boilerplate.
Customizing Bootstrap Styles
Sometimes, the out-of-the-box look might not entirely suit the aesthetic needs of your application. Customizing Bootstrap can be done by overriding its Sass variables.
Using Sass to Customize Bootstrap
First, you need to install node-sass.
1npm install node-sassNext, rename index.css to index.scss, and import Bootstrap’s Sass files in it.
1@import 'node_modules/bootstrap/scss/bootstrap';
2
3// Your custom styles...
4$primary-color: #ff6347;
5
6body {
7 background-color: $primary-color;
8}This change makes your background color match Bootstrap’s primary color, showcasing how you can easily override Bootstrap variables.
Bootstrap Forms in React
Forms are crucial in most web applications, and Bootstrap makes it easier to style and validate them.
1import React, { useState } from 'react';
2import { Form, Button } from 'react-bootstrap';
3
4function App() {
5 const [validated, setValidated] = useState(false);
6
7 const handleSubmit = (event) => {
8 const form = event.currentTarget;
9 if (form.checkValidity() === false) {
10 event.preventDefault();
11 event.stopPropagation();
12 }
13 setValidated(true);
14 };
15
16 return (
17 <Form noValidate validated={validated} onSubmit={handleSubmit}>
18 <Form.Group controlId="formBasicEmail">
19 <Form.Label>Email address</Form.Label>
20 <Form.Control required type="email" placeholder="Enter email" />
21 <Form.Control.Feedback type="invalid">
22 Please provide a valid email.
23 </Form.Control.Feedback>
24 </Form.Group>
25
26 <Form.Group controlId="formBasicPassword">
27 <Form.Label>Password</Form.Label>
28 <Form.Control required type="password" placeholder="Password" />
29 <Form.Control.Feedback type="invalid">
30 Please provide a password.
31 </Form.Control.Feedback>
32 </Form.Group>
33
34 <Button variant="primary" type="submit">
35 Submit
36 </Button>
37 </Form>
38 );
39}
40
41export default App;This example showcases a form with basic validation handled by Bootstrap classes and React state management.
Integrating Bootstrap with React is a powerful way to accelerate your front-end development. Bootstrap provides a comprehensive suite of components and utilities, and React-Bootstrap translates them into React components. This synergy allows you to combine React’s dynamic capabilities with Bootstrap’s robust styling, elevating both your development workflow and user experience.
Explore the React-Bootstrap documentation and Bootstrap documentation further to unlock the full potential of this combination in your projects.