DEV Community

Awais Abbas
Awais Abbas

Posted on • Updated on

Starting with react-bootstrap

Alt Text

Hi folks! This is a simple guide for the ones facing trouble setting up react-bootstrap. I will walk you through the basic steps one-by-one with some examples. So, stick together and lets rock-n-roll :)

Creating a React Project

Installing react-app

"create-react-app" is an npm package that sets up the react project for you. Open the terminal and type:

npm install -g create-react-app
yarn add create-react-app (If you use Yarn)

Tip : To check if "package is already installed on system", type:
npm list -g "package-name"

Lets create the project with "create-react-app". Open terminal and type:
npx create-react-app testing-project
npm init react-app testing-project
yarn create react-app testing-project

Then in terminal type:

cd testing-project
npm start

"npm start" runs the project if it is successfully built.

Installing react-bootstrap

Open the project in the text-editor (VsCode in my case) and type in the terminal:
npm install react-bootstrap bootstrap
Tip : To open the terminal in VsCode, shortcut key is " ctrl + ~ "

Navigate to index.js" inside the /src folder and add the following line to import all styles from "bootstrap"

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

All done😀. Lets test it out.


Simple Button Component

Go to the app.js file and import a bootstrap-react-component and the remaining code;
import {Button} from 'react-bootstrap'

import React from "react";
import { Button } from "react-bootstrap";

function App() {
  return (
    <div className="my-4">
      <Button variant="primary" className="mx-4">
        Primary Button
      <Button variant="secondary">Secondary button</Button>

export default App;
Enter fullscreen mode Exit fullscreen mode

Run the project if its not running. Go to terminal and type :

npm start

Here is the ouput: Woaaaah🥳🥳🥳

Alt Text

  • my & mx are used for applying margins in bootstrap
  • variant is similar to bootstrap classes but its specifically used inside the bootstrap-react-components"

Tip : Another way of importing Component in react-bootstrap is this:

import Button from 'react-bootstrap/Button'

which is a bit complex as compared to like we imported earlier.We used Destructuring to grab the component which we wanted from whole library of react-bootstrap.Similarly this method can be used to import other components like in the another example below.

Another Example: Card Component

import React from "react";
import { CardGroup, Card, Container } from "react-bootstrap";

function App() {
  return (
      <Container className="mt-5">
              <Card.Title>Card title</Card.Title>
                This is a wider card with supporting text below as a natural
                lead-in to additional content. This content is a little bit
              <small className="text-muted">Last updated 3 mins ago</small>
Enter fullscreen mode Exit fullscreen mode

Lets break it down a bit :)

  • First we destructured the Components we wanted as CardGroup, Card, Container.
  • Container gives us the standard margins and some decrease in width.
  • Now Inside the Card component we can use all the available classes which are written with dot after the component to provide all the properties.

Following output is provided after running the script:
Alt Text


To see all the Component Examples like above...Visit the official React-bootstrap website

Top comments (0)