Author avatar

Gaurav Singhal

How to Import Components from React Bootstrap

Gaurav Singhal

  • May 18, 2020
  • 6 Min read
  • 733 Views
  • May 18, 2020
  • 6 Min read
  • 733 Views
Web Development
Front End Web Development
Client-side Framework
React

Introduction

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.

Adding React Bootstrap into a React App

Install the React Bootstrap library by using the below npm command.

1
npm install react-bootstrap bootstrap
powershell

After installing the above two libraries, the next step is to add bootstrap CSS file into either index.js or app.js like this:

1
import 'bootstrap/dist/css/bootstrap.min.css';
js

Alternatively, if you are using SASS in your app, you can add it as shown below.

App.scss

1
@import "~bootstrap/scss/bootstrap";
js

App.js

1
import './App.scss';
js

Now you are good to go and can proceed further with React Bootstrap by importing various components into your React app.

Importing Components from React Bootstrap

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:

1
import { COMPONENT_NAME } from 'react-bootstrap';
jsx

Or:

1
import COMPONENT_NAME from 'react-bootstrap/COMPONENT_NAME';
jsx

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.

Import Components from the React Bootstrap Library

Importing Single Components

You can import any single component from react-bootstrap as explained below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import React, { Component } from "react";
import { Image } from "react-bootstrap";

class SingleComponent extends Component {
  render() {
    return (
      <div>
        <Image
          src="https://dummyimage.com/170x180/000/fff.png&text=TEST123"
          thumbnail
        />
      </div>
    );
  }
}

export default SingleComponent;
jsx

In the above example, to import the single component called Image from the react-bootstrap library, the command used is:

1
import { Image } from "react-bootstrap";
jsx

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:

  • Import Single Components Individually

You can import and use any React Bootstrap components individually from react-bootstrap rather than importing them from the complete library as shown below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import React, { Component } from "react";
import Breadcrumb from "react-bootstrap/Breadcrumb";

class SingleIndividualComp extends Component {
  render() {
    return (
      <Breadcrumb>
        <Breadcrumb.Item href="#">Home</Breadcrumb.Item>
        <Breadcrumb.Item href="#">Profile</Breadcrumb.Item>
        <Breadcrumb.Item active>Test123</Breadcrumb.Item>
      </Breadcrumb>
    );
  }
}

export default SingleIndividualComp;
jsx

As shown in the above example, Breadcrumb individually imported by using this command:

1
import Breadcrumb from "react-bootstrap/Breadcrumb";
jsx

Pulling the specific component from the library rather than picking up the whole package improves the performance while fetching and rendering the particular component.

  • Use React Bootstrap Default Syntax

The default syntax is a way to use React Bootstrap dynamically without naming the specific components, as done previously.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import React, { Component } from "react";
import * as ReactBootstrap from "react-bootstrap";

class DynamicImport extends Component {
  render() {
    return (
      <ReactBootstrap.Button bsStyle="success" bsSize="small">
        Something
      </ReactBootstrap.Button>
    );
  }
}

export default DynamicImport;
jsx

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.

1
import * as ReactBootstrap from "react-bootstrap";
jsx

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.

Importing Multiple Components

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import React, { Component } from "react";
import { Form, Col, Button } from "react-bootstrap";

class MultipleImport extends Component {
  render() {
    return (
      <Form>
        <Form.Row>
          <Col>
            <Form.Control placeholder="First name..." />
          </Col>
          <Col>
            <Form.Control placeholder="Last name..." />
          </Col>
          <Col>
            <Button>Submit</Button>
          </Col>
        </Form.Row>
      </Form>
    );
  }
}

export default MultipleImport;
jsx

In the above example, three components, Form, Col, and Button, are imported from the single import statement.

1
import { Form, Col, Button } from "react-bootstrap";
jsx

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.

Conclusion

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.

1