React Bootstrap is one of the widely used libraries in React, and its various components are used in React apps to make them mobile-friendly. It has tons of components that give apps the various details of layout, form controls, information indicators, and navigational components.
In this guide, you are going to learn how to add React Bootstrap into a React app by importing and using the React Bootstrap components in your React components.
Install the React Bootstrap library by using the below npm
command.
1npm install react-bootstrap bootstrap
After installing the above two libraries, the next step is to add bootstrap CSS file into either index.js
or app.js
like this:
1import 'bootstrap/dist/css/bootstrap.min.css';
Alternatively, if you are using SASS in your app, you can add it as shown below.
App.scss
1@import "~bootstrap/scss/bootstrap";
App.js
1import './App.scss';
Now you are good to go and can proceed further with React Bootstrap by importing various components into your React app.
Now that you have installed the bootstrap library and configured the CSS file into your app, it’s time to import the components from react-bootstrap
.
The basic syntax for importing anything from react-bootstrap
looks like this:
1import { COMPONENT_NAME } from 'react-bootstrap';
Or:
1import COMPONENT_NAME from 'react-bootstrap/COMPONENT_NAME';
It is the basic syntax that’s used to import the specific components from the library, but you can still import it using other ways that you will learn in the next section of this guide.
You can import any single component from react-bootstrap
as explained below.
1import React, { Component } from "react";
2import { Image } from "react-bootstrap";
3
4class SingleComponent extends Component {
5 render() {
6 return (
7 <div>
8 <Image
9 src="https://dummyimage.com/170x180/000/fff.png&text=TEST123"
10 thumbnail
11 />
12 </div>
13 );
14 }
15}
16
17export default SingleComponent;
In the above example, to import the single component called Image
from the react-bootstrap
library, the command used is:
1import { Image } from "react-bootstrap";
Inside the render()
function, the <Image/>
component is used along with various properties like href
and another supporting property called thumbnail
.
This is how you can import any single component from React Bootstrap:
You can import and use any React Bootstrap components individually from react-bootstrap
rather than importing them from the complete library as shown below.
1import React, { Component } from "react";
2import Breadcrumb from "react-bootstrap/Breadcrumb";
3
4class SingleIndividualComp extends Component {
5 render() {
6 return (
7 <Breadcrumb>
8 <Breadcrumb.Item href="#">Home</Breadcrumb.Item>
9 <Breadcrumb.Item href="#">Profile</Breadcrumb.Item>
10 <Breadcrumb.Item active>Test123</Breadcrumb.Item>
11 </Breadcrumb>
12 );
13 }
14}
15
16export default SingleIndividualComp;
As shown in the above example, Breadcrumb
individually imported by using this command:
1import Breadcrumb from "react-bootstrap/Breadcrumb";
Pulling the specific component from the library rather than picking up the whole package improves the performance while fetching and rendering the particular component.
The default syntax is a way to use React Bootstrap dynamically without naming the specific components, as done previously.
1import React, { Component } from "react";
2import * as ReactBootstrap from "react-bootstrap";
3
4class DynamicImport extends Component {
5 render() {
6 return (
7 <ReactBootstrap.Button bsStyle="success" bsSize="small">
8 Something
9 </ReactBootstrap.Button>
10 );
11 }
12}
13
14export default DynamicImport;
The above example does not import any components by name but used a dynamic import format to fetch a specific component when needed, as shown below.
1import * as ReactBootstrap from "react-bootstrap";
When you need any components while developing the UI, you have to use prefix ReactBoootstrap.comp_name
followed by the specific component name, which makes it easier to use.
So far in this guide, you have learned how to import a single specific component, but you can import multiple components from the single import statement as well.
Below is a simple example that shows how to import multiple components from react-bootstrap
from a single import statement.
1import React, { Component } from "react";
2import { Form, Col, Button } from "react-bootstrap";
3
4class MultipleImport extends Component {
5 render() {
6 return (
7 <Form>
8 <Form.Row>
9 <Col>
10 <Form.Control placeholder="First name..." />
11 </Col>
12 <Col>
13 <Form.Control placeholder="Last name..." />
14 </Col>
15 <Col>
16 <Button>Submit</Button>
17 </Col>
18 </Form.Row>
19 </Form>
20 );
21 }
22}
23
24export default MultipleImport;
In the above example, three components, Form
, Col
, and Button
, are imported from the single import statement.
1import { Form, Col, Button } from "react-bootstrap";
It’s pretty handy sometimes because you don’t need to have multiple import statements that import a single component from the same library concurrently.
In this guide, you have learned the various methods for importing components, like importing single components, individual components, dynamic components, and multiple components using a single import statement.
You can choose any of these methods, but when it comes to the performance, the multiple components import statement or individual component import are way better than the others. I hope this guide helps you to play around importing various components. That was it from this guide; be safe, and keep coding.