Categories
React Bootstrap

React Bootstrap — Badges, Breadcrumbs, and Buttons

React Bootstrap is one version of Bootstrap made for React.

It’s a set of React components that have Bootstrap styles.

In this article, we’ll look at how to work with React Bootstrap’s badges, breadcrumbs, and buttons in our React app.

Badges

Badges are components that lets us display some text in a box beside other text.

It’s sized according to the neighboring text.

For instance, we can use it by writing:

import React from "react";
import Badge from "react-bootstrap/Badge";
import "bootstrap/dist/css/bootstrap.min.css";

export default function App() {
  return (
    <>
      <div>
        <h1>
          heading <Badge variant="primary">New</Badge>
        </h1>
        <h2>
          heading <Badge variant="secondary">New</Badge>
        </h2>
        <h3>
          heading <Badge variant="warning">New</Badge>
        </h3>
        <h4>
          heading <Badge variant="success">New</Badge>
        </h4>
        <h5>
          heading <Badge variant="danger">New</Badge>
        </h5>
        <h6>
          heading <Badge variant="secondary">New</Badge>
        </h6>
      </div>
    </>
  );
}

We have the variant prop that lets us change the background color.

The size of it will match the neighboring text.

Contextual Variations

We can change the variant prop to style the badge the way we want.

For example, we can write:

import React from "react";
import Badge from "react-bootstrap/Badge";
import "bootstrap/dist/css/bootstrap.min.css";

export default function App() {
  return (
    <>
      <div>
        <p>
          heading <Badge variant="primary">New</Badge>
        </p>
        <p>
          heading <Badge variant="secondary">New</Badge>
        </p>
        <p>
          heading <Badge variant="warning">New</Badge>
        </p>
        <p>
          heading <Badge variant="success">New</Badge>
        </p>
        <p>
          heading <Badge variant="danger">New</Badge>
        </p>
        <p>
          heading <Badge variant="dark">New</Badge>
        </p>
        <p>
          heading <Badge variant="light">New</Badge>
        </p>
      </div>
    </>
  );
}

We change the variant to those values so that they have different background colors.

Pill

Also, we can change the style to the pill style, which is rounder than the regular badge.

For example, we can write:

import React from "react";
import Badge from "react-bootstrap/Badge";
import "bootstrap/dist/css/bootstrap.min.css";

export default function App() {
  return (
    <>
      <div>
        <p>
          heading{" "}
          <Badge pill variant="primary">
            New
          </Badge>
        </p>
        <p>
          heading{" "}
          <Badge pill variant="secondary">
            New
          </Badge>
        </p>
        <p>
          heading{" "}
          <Badge pill variant="warning">
            New
          </Badge>
        </p>
        <p>
          heading{" "}
          <Badge pill variant="success">
            New
          </Badge>
        </p>
        <p>
          heading{" "}
          <Badge pill variant="danger">
            New
          </Badge>
        </p>
        <p>
          heading{" "}
          <Badge pill variant="dark">
            New
          </Badge>
        </p>
        <p>
          heading{" "}
          <Badge pill variant="light">
            New
          </Badge>
        </p>
      </div>
    </>
  );
}

We add the pill prop so that we get rounded badges.

Breadcrumbs

We can add breadcrumbs to show the navigation hierarchy with separators separating each level.

For example, we can write:

import React from "react";
import Breadcrumb from "react-bootstrap/Breadcrumb";
import "bootstrap/dist/css/bootstrap.min.css";

export default function App() {
  return (
    <>
      <Breadcrumb>
        <Breadcrumb.Item href="#">Home</Breadcrumb.Item>
        <Breadcrumb.Item href="https://example.com/">Profile</Breadcrumb.Item>
        <Breadcrumb.Item active>Data</Breadcrumb.Item>
      </Breadcrumb>
    </>
  );
}

We have the Breadcrumb component with Breadcrumb.Item components inside.

href has the URL to go when we click on the breadcrumb link.

Buttons

React Bootstrap provides us with multiple styles of buttons.

We can change the style with the variant prop.

For example, we can write:

import React from "react";
import Button from "react-bootstrap/Button";
import "bootstrap/dist/css/bootstrap.min.css";

export default function App() {
  return (
    <>
      <Button variant="primary">button</Button>{" "}
      <Button variant="secondary">button</Button>{" "}
      <Button variant="success">button</Button>{" "}
      <Button variant="warning">button</Button>{" "}
      <Button variant="danger">button</Button>{" "}
      <Button variant="info">button</Button>{" "}
      <Button variant="light">button</Button>{" "}
      <Button variant="dark">button</Button>{" "}
      <Button variant="link">Link</Button>
    </>
  );
}

We have different varieties of buttons with different background colors.

variant has the background color of the button.

If the variant is link , then it’s displayed as a link.

Outline Buttons

We can make buttons have a light background and colored outline with the outline- prefix for the variant prop.

For example, we can write:

import React from "react";
import Button from "react-bootstrap/Button";
import "bootstrap/dist/css/bootstrap.min.css";

export default function App() {
  return (
    <>
      <Button variant="outline-primary">button</Button>{" "}
      <Button variant="outline-secondary">button</Button>{" "}
      <Button variant="outline-success">button</Button>{" "}
      <Button variant="outline-warning">button</Button>{" "}
      <Button variant="outline-danger">button</Button>{" "}
      <Button variant="outline-info">button</Button>{" "}
      <Button variant="outline-light">button</Button>{" "}
      <Button variant="outline-dark">button</Button>{" "}
      <Button variant="outline-link">Link</Button>
    </>
  );
}

Now we get buttons with the outline colored and white background.

Button Tags

We can change the tag that a Button is rendered as with the as prop.

For instance, we can write:

import React from "react";
import Button from "react-bootstrap/Button";
import "bootstrap/dist/css/bootstrap.min.css";

export default function App() {
  return (
    <>
      <Button as="input" type="button" value="button" />{" "}
      <Button as="input" type="submit" value="submit" />{" "}
      <Button as="input" type="reset" value="reset" />
    </>
  );
}

We have the as prop to render the component we the input element.

Then we specified the type prop for the input field type.

The value has the text content of the button.

Conclusion

Badges let us display a small piece of text beside another piece of text.

Also, breadcrumbs let us display navigation hierarchy with links.

And buttons can be styled and rendered in various ways.

Categories
React Bootstrap

React Bootstrap — Alerts

React Bootstrap is one version of Bootstrap made for React.

It’s a set of React components that have Bootstrap styles.

In this article, we’ll look at how to work with React Bootstrap’s alerts in our React components.

Alerts

Alerts provide us with feedback for user actions with various styling.

For instance, we can write:

import React from "react";
import Alert from "react-bootstrap/Alert";
import "bootstrap/dist/css/bootstrap.min.css";

export default function App() {
  return (
    <>
      {[
        "primary",
        "secondary",
        "success",
        "danger",
        "warning",
        "info",
        "light",
        "dark"
      ].map((variant, index) => (
        <Alert key={index} variant={variant}>
          {variant}
        </Alert>
      ))}
    </>
  );
}

to display all varieties of alerts.

Also, we can add Alert.Link components to add links to the alerts.

For example, we can write:

import React from "react";
import Alert from "react-bootstrap/Alert";
import "bootstrap/dist/css/bootstrap.min.css";

export default function App() {
  return (
    <>
      {[
        "primary",
        "secondary",
        "success",
        "danger",
        "warning",
        "info",
        "light",
        "dark"
      ].map((variant, idx) => (
        <Alert key={idx} variant={variant}>
          {variant} <Alert.Link href="https://example.com">link</Alert.Link>.
        </Alert>
      ))}
    </>
  );
}

We used the Alert.Link component to add a link with a href to go to a URL.

Additional Content

We can also add whatever content we want.

There’s the Alert.Heading component to add a heading to the alert.

And the content can come below it.

For example, we can write:

import React from "react";
import Alert from "react-bootstrap/Alert";
import "bootstrap/dist/css/bootstrap.min.css";

export default function App() {
  return (
    <>
      {[
        "primary",
        "secondary",
        "success",
        "danger",
        "warning",
        "info",
        "light",
        "dark"
      ].map((variant, idx) => (
        <Alert variant={variant} key={idx}>
          <Alert.Heading>Title</Alert.Heading>
          <p>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent
            ultrices ac dolor nec vestibulum. Maecenas vulputate diam ut sem
            tempus, id eleifend tortor hendrerit. Sed non orci massa. Aliquam
            eget lectus a ante convallis gravida. Donec fringilla odio ut magna
            gravida aliquam. Cras molestie non ante vel dictum. Cras lacinia
            molestie lacus, in lacinia sapien imperdiet in. Sed eleifend laoreet
            finibus. Integer semper dictum eros nec eleifend. Nunc quam mi,
            finibus lacinia metus vitae, dapibus ultricies diam. Vivamus ante
            nisi, pellentesque at lacus eu, vehicula pretium lorem. Nunc vitae
            ligula laoreet, accumsan diam et, auctor mauris. Fusce vitae posuere
            nibh, vitae eleifend quam. Duis a enim lacus.
          </p>
          <hr />
          <p className="mb-0">
            Integer non ante ut arcu imperdiet maximus. Pellentesque id metus
            porttitor, ornare ex et, finibus ante. Aenean mattis ligula lectus,
            a aliquet est pharetra et. Donec sit amet est massa. Maecenas dolor
            ante, congue sit amet mattis eu, vehicula quis nisl. Nulla viverra
            ligula vitae mollis sagittis. Fusce volutpat convallis purus.
            Vestibulum tincidunt elit id aliquam placerat. Vivamus vestibulum
            enim sed eros ullamcorper congue. Fusce nec tincidunt arcu. Sed sed
            suscipit justo, ac eleifend mauris. Morbi molestie turpis a accumsan
            mollis. Aenean vel diam vitae ante tincidunt varius.
          </p>
        </Alert>
      ))}
    </>
  );
}

We added an alert with a heading in the Alert.Heading component and some paragraphs below it.

Dismissing Alerts

We can make alerts dismissible with the dismissible prop.

For instance, we can write:

import React from "react";
import Alert from "react-bootstrap/Alert";
import Button from "react-bootstrap/Button";
import "bootstrap/dist/css/bootstrap.min.css";

export default function App() {
  const [show, setShow] = React.useState(true);

return (
    <>
      <Button onClick={() => setShow(true)}>Show Alert</Button>
      {show && (
        <Alert variant="success" onClose={() => setShow(false)} dismissible>
          <Alert.Heading>Title</Alert.Heading>
          <p>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent
            ultrices ac dolor nec vestibulum. Maecenas vulputate diam ut sem
            tempus, id eleifend tortor hendrerit. Sed non orci massa. Aliquam
            eget lectus a ante convallis gravida. Donec fringilla odio ut magna
            gravida aliquam. Cras molestie non ante vel dictum. Cras lacinia
            molestie lacus, in lacinia sapien imperdiet in. Sed eleifend laoreet
            finibus. Integer semper dictum eros nec eleifend. Nunc quam mi,
            finibus lacinia metus vitae, dapibus ultricies diam. Vivamus ante
            nisi, pellentesque at lacus eu, vehicula pretium lorem. Nunc vitae
            ligula laoreet, accumsan diam et, auctor mauris. Fusce vitae posuere
            nibh, vitae eleifend quam. Duis a enim lacus.
          </p>
          <hr />
          <p className="mb-0">
            Integer non ante ut arcu imperdiet maximus. Pellentesque id metus
            porttitor, ornare ex et, finibus ante. Aenean mattis ligula lectus,
            a aliquet est pharetra et. Donec sit amet est massa. Maecenas dolor
            ante, congue sit amet mattis eu, vehicula quis nisl. Nulla viverra
            ligula vitae mollis sagittis. Fusce volutpat convallis purus.
            Vestibulum tincidunt elit id aliquam placerat. Vivamus vestibulum
            enim sed eros ullamcorper congue. Fusce nec tincidunt arcu. Sed sed
            suscipit justo, ac eleifend mauris. Morbi molestie turpis a accumsan
            mollis. Aenean vel diam vitae ante tincidunt varius.
          </p>
        </Alert>
      )}
    </>
  );
}

We added the show state to show the alert if it’s true .

And we have a Button to make show true when clicked.

The dismissible on the Alert lets us close the alert.

The onClose prop takes a callback that lets us close the alert by setting the show state to false .

It’ll be run when the close button on the alert is clicked.

This is shown since we added the dismissble prop.

Conclusion

Alerts are boxes that let us show messages triggered by user actions.

We can show whatever we want in them and there’re several varieties of them.

Categories
React Bootstrap

React Bootstrap — Accordion

React Bootstrap is one version of Bootstrap made for React.

It’s a set of React components that have Bootstrap styles.

In this article, we’ll look at how to work with React Bootstrap’s accordions in our React components.

Accordion

Accordions let us restrict card components to open only one at a time.

To use it, we can write:

import React from "react";
import Accordion from "react-bootstrap/Accordion";
import Card from "react-bootstrap/Card";
import Button from "react-bootstrap/Button";
import "bootstrap/dist/css/bootstrap.min.css";

export default function App() {
  return (
    <>
      <Accordion defaultActiveKey="0">
        <Card>
          <Card.Header>
            <Accordion.Toggle as={Button} variant="link" eventKey="0">
              title 1
            </Accordion.Toggle>
          </Card.Header>
          <Accordion.Collapse eventKey="0">
            <Card.Body>body 1</Card.Body>
          </Accordion.Collapse>
        </Card>
        <Card>
          <Card.Header>
            <Accordion.Toggle as={Button} variant="link" eventKey="1">
              title 2
            </Accordion.Toggle>
          </Card.Header>
          <Accordion.Collapse eventKey="1">
            <Card.Body>body 2</Card.Body>
          </Accordion.Collapse>
        </Card>
      </Accordion>
    </>
  );
}

We imported the Accordion and Card components.

It has the defaultActionKey is the key for the default card.

Accordion.Toggle is the toggle to open the card.

as is the prop to let us specify what component to render the toggle as.

variant is the style that we want to render the toggle as.

eventKey is the key for the given card.

Fully Collapsed State

If we don’t specify the defaultActiveKey , then all cards will be collapsed.

For example, we can write:

import React from "react";
import Accordion from "react-bootstrap/Accordion";
import Card from "react-bootstrap/Card";
import Button from "react-bootstrap/Button";
import "bootstrap/dist/css/bootstrap.min.css";

export default function App() {
  return (
    <>
      <Accordion>
        <Card>
          <Card.Header>
            <Accordion.Toggle as={Button} variant="link" eventKey="0">
              title 1
            </Accordion.Toggle>
          </Card.Header>
          <Accordion.Collapse eventKey="0">
            <Card.Body>body 1</Card.Body>
          </Accordion.Collapse>
        </Card>
        <Card>
          <Card.Header>
            <Accordion.Toggle as={Button} variant="link" eventKey="1">
              title 2
            </Accordion.Toggle>
          </Card.Header>
          <Accordion.Collapse eventKey="1">
            <Card.Body>body 2</Card.Body>
          </Accordion.Collapse>
        </Card>
      </Accordion>
    </>
  );
}

Make the Entire Header Clickable

We can make the entire header clickable by rendering the header as a Card.Header .

For instance, we can write:

import React from "react";
import Accordion from "react-bootstrap/Accordion";
import Card from "react-bootstrap/Card";
import "bootstrap/dist/css/bootstrap.min.css";

export default function App() {
  return (
    <>
      <Accordion>
        <Card>
          <Card.Header>
            <Accordion.Toggle as={Card.Header} variant="link" eventKey="0">
              title 1
            </Accordion.Toggle>
          </Card.Header>
          <Accordion.Collapse eventKey="0">
            <Card.Body>body 1</Card.Body>
          </Accordion.Collapse>
        </Card>
        <Card>
          <Card.Header>
            <Accordion.Toggle as={Card.Header} variant="link" eventKey="1">
              title 2
            </Accordion.Toggle>
          </Card.Header>
          <Accordion.Collapse eventKey="1">
            <Card.Body>body 2</Card.Body>
          </Accordion.Collapse>
        </Card>
      </Accordion>
    </>
  );
}

We make the part outside the text clickable by changing the as to Card.Header .

Custom Toggle

We can create our custom toggle component with the useAccordionToggle hook.

It takes the eventKey and a callback that runs when the roggle is clicked.

For instance, we can write:

import React from "react";
import Accordion from "react-bootstrap/Accordion";
import Card from "react-bootstrap/Card";
import { useAccordionToggle } from "react-bootstrap/AccordionToggle";
import "bootstrap/dist/css/bootstrap.min.css";

function CustomToggle({ children, eventKey }) {
  const decoratedOnClick = useAccordionToggle(eventKey, () =>
    console.log("toggle clicked")
  );

  return (
    <button
      type="button"
      style={{ backgroundColor: "orange" }}
      onClick={decoratedOnClick}
    >
      {children}
    </button>
  );
}

export default function App() {
  return (
    <>
      <Accordion>
        <Card>
          <Card.Header>
            <CustomToggle eventKey="0">title 1</CustomToggle>
          </Card.Header>
          <Accordion.Collapse eventKey="0">
            <Card.Body>body 1</Card.Body>
          </Accordion.Collapse>
        </Card>
        <Card>
          <Card.Header>
            <CustomToggle eventKey="0">title 2</CustomToggle>
          </Card.Header>
          <Accordion.Collapse eventKey="1">
            <Card.Body>body 2</Card.Body>
          </Accordion.Collapse>
        </Card>
      </Accordion>
    </>
  );
}

We have the CustomToggle component which we created ourselves.

It takes the children and eventKey props.

children lets us display content that we pass inside the component tags.

useAccordingToggle returns a click handler that opens the card with the given eventKey .

The 2nd argument is a callback that lets us run anything when the toggle is clicked.

Then we can use the CustomToggle in place of the toggle that comes with React Bootstrap to toggle our cards.

Custom Toggle with Expansion Awareness

We can also check if the toggle has expanded the card when it’s clicked.

To do that, we use the context API to listen to the accordion to the AccordionContext .

For instance, we can write:

import React from "react";
import Accordion from "react-bootstrap/Accordion";
import AccordionContext from "react-bootstrap/AccordionContext";
import Card from "react-bootstrap/Card";
import { useAccordionToggle } from "react-bootstrap/AccordionToggle";
import "bootstrap/dist/css/bootstrap.min.css";

function CustomToggle({ children, eventKey, callback }) {
  const currentEventKey = React.useContext(AccordionContext);

const decoratedOnClick = useAccordionToggle(
    eventKey,
    () => callback && callback(eventKey)
  );

const isCurrentEventKey = currentEventKey === eventKey;

return (
    <button
      type="button"
      style={{ backgroundColor: isCurrentEventKey ? "orange" : "pink" }}
      onClick={decoratedOnClick}
    >
      {children}
    </button>
  );
}

export default function App() {
  return (
    <>
      <Accordion>
        <Card>
          <Card.Header>
            <CustomToggle eventKey="0">title 1</CustomToggle>
          </Card.Header>
          <Accordion.Collapse eventKey="0">
            <Card.Body>body 1</Card.Body>
          </Accordion.Collapse>
        </Card>
        <Card>
          <Card.Header>
            <CustomToggle eventKey="1">title 2</CustomToggle>
          </Card.Header>
          <Accordion.Collapse eventKey="1">
            <Card.Body>body 2</Card.Body>
          </Accordion.Collapse>
        </Card>
      </Accordion>
    </>
  );
}

We get the eventKey prop with our CustomToggle so that we can compare to the currentEventKey that we have in our context.

Then we have the useAccordionToggle as we did with the previous example.

It returns a click handler that we pass into onClick to expand the card.

isCurrentKey has the result of comparing the eventKey of the toggle with the one from the context which has the one that’s opened.

So we can use that to style the toggle the way we like when the card is open or closed.

Conclusion

Accordions let us restrict card components to open only one at a time.

We can customize the toggle and the card content.

Categories
React Bootstrap

Getting Started with React Bootstrap

React Bootstrap is one version of Bootstrap made for React.

It’s a set of React components that have Bootstrap styles.

In this article, we’ll look at how to get started with React Bootstrap.

Installation

We can install React Bootstrap by running:

npm install react-bootstrap bootstrap

Importing Components

Once we imported our components, we can add the component we want by writing:

import React from "react";
import Button from "react-bootstrap/Button";
import "bootstrap/dist/css/bootstrap.min.css";

export default function App() {
  return (
    <div className="App">
      <Button>button</Button>
    </div>
  );
}

We import the CSS and the button so we can add a button with the Boostrap styles.

We can also import the SASS code by writing:

@import "~bootstrap/scss/bootstrap";

This works with create-react-app apps.

Bootstrap with State

We can pass states to React Bootstrap components as we do with any other React component.

For instance, we can write:

import React from "react";
import Alert from "react-bootstrap/Alert";
import Button from "react-bootstrap/Button";
import "bootstrap/dist/css/bootstrap.min.css";

export default function App() {
  const [show, setShow] = React.useState(true);

  return (
    <>
      <Button onClick={() => setShow(show => !show)} variant="outline-success">
        toggle alert
      </Button>
      <Alert show={show} variant="success">
        <Alert.Heading>title</Alert.Heading>
        <p>alert</p>
      </Alert>
    </>
  );
}

We have the show state that we use to toggle the alert.

We toggle the alert with the Button by running setShow to toggle the show state when we click the button.

Theming and Customizing Styles

We can style our components the ay we like.

For instance, we can write:

import React from "react";
import Button from "react-bootstrap/Button";
import "bootstrap/dist/css/bootstrap.min.css";

export default function App() {
  return (
    <>
      <style type="text/css">
        {`
          .btn-flat {
            background-color: green;
            color: white;
          }

          .btn-xxl {
            padding: 1rem 1.5rem;
            font-size: 2.5rem;
          }
    `   }
      </style>

      <Button variant="flat" size="xxl">
        button
      </Button>
    </>
  );
}

Button takes a variant and size prop that lets us style it however we like.

Other components also have these options.

variant is the button variant, which corresponds to the name after the prefix.

size is also after the prefix.

We have the btn-flat and btn-xxl classes that we can use to style the buttons.

We have the style element to apply the styles with a CSS string.

Prefixing Components

We can change the prefix of the class with the bsPrefix prop.

Or we can do the same globally with the ThemeProvider component.

For instance, we can write:

import React from "react";
import Alert from "react-bootstrap/Alert";
import Button from "react-bootstrap/Button";
import "bootstrap/dist/css/bootstrap.min.css";

export default function App() {
  return (
    <>
      <style type="text/css">
        {`
         .super-btn-flat {
           background-color: green;
           color: white;
         }

         .super-btn-xxl {
           padding: 1rem 1.5rem;
           font-size: 2.5rem;
         }
       `}
      </style>

      <Button bsPrefix="super-btn" variant="flat" size="xxl">
        button
      </Button>
    </>
  );
}

We change bsPrefix to super-btn , so all the class names of the button are prefixed with super-btn .

Therefore, our styles all have this prefix as their selectors.

We can do the same globally with the ThemeProvider as follows:

import React from "react";
import ThemeProvider from "react-bootstrap/ThemeProvider";
import Button from "react-bootstrap/Button";
import "bootstrap/dist/css/bootstrap.min.css";

export default function App() {
  return (
    <>
      <style type="text/css">
        {`
          .super-btn-flat {
            background-color: green;
            color: white;
          }

          .super-btn-xxl {
            padding: 1rem 1.5rem;
            font-size: 2.5rem;
          }
       `}
      </style>
      <ThemeProvider prefixes={{ btn: "super-btn" }}>
        <Button variant="flat" size="xxl">
          button
        </Button>
      </ThemeProvider>
    </>
  );
}

We pass in an object to the prefixes prop.

And we set the btn property to super-btn to change the class name prefix of the button.

Conclusion

We can use React Bootstrap as a widget library for our React apps.

It provides styled React components that are styled in the Bootstrap style.

We can theme them with global styles or apply our local styles to them.

Categories
React Bootstrap

React Bootstrap — Spinners and Tables

React Bootstrap is one version of Bootstrap made for React.

It’s a set of React components that have Bootstrap styles.

In this article, we’ll look at how to add spinners with React Bootstrap.

Spinners

Spinners are used to show the loading state of our data.

To use it, we can use the Spinner component:

import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import Spinner from "react-bootstrap/Spinner";

export default function App() {
  return (
    <>
      <Spinner animation="border" role="status">
        <span className="sr-only">Loading...</span>
      </Spinner>
    </>
  );
}

We add the Spinner with the animation prop.

border means that we have a circle that spins as the spinner.

We can also add text for screen readers with the sr-only class.

It’ll be read by screen readers but users won’t see them.

Animations

We can also set the animation prop to grow .

Then we’ll get a throbbing spinner:

import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import Spinner from "react-bootstrap/Spinner";

export default function App() {
  return (
    <>
      <Spinner animation="grow" role="status">
        <span className="sr-only">Loading...</span>
      </Spinner>
    </>
  );
}

Variants

There’re various styling variants for spinners.

All the common Bootstrap ones are available.

For instance, we can write:

import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import Spinner from "react-bootstrap/Spinner";

export default function App() {
  return (
    <>
      <Spinner animation="border" variant="primary" />
      <Spinner animation="border" variant="secondary" />
      <Spinner animation="border" variant="success" />
      <Spinner animation="border" variant="danger" />
      <Spinner animation="border" variant="warning" />
      <Spinner animation="border" variant="info" />
      <Spinner animation="border" variant="light" />
      <Spinner animation="border" variant="dark" />
      <Spinner animation="grow" variant="primary" />
      <Spinner animation="grow" variant="secondary" />
      <Spinner animation="grow" variant="success" />
      <Spinner animation="grow" variant="danger" />
      <Spinner animation="grow" variant="warning" />
      <Spinner animation="grow" variant="info" />
      <Spinner animation="grow" variant="light" />
      <Spinner animation="grow" variant="dark" />
    </>
  );
}

Then we get spinners with different colors and animation styles.

Sizes

We can also change the sizes of the spinner,

For instance, we can write:

import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import Spinner from "react-bootstrap/Spinner";

export default function App() {
  return (
    <>
      <Spinner animation="border" size="sm" />
      <Spinner animation="border" />
      <Spinner animation="grow" size="sm" />
      <Spinner animation="grow" />
    </>
  );
}

to set the size with the size prop.

sm means smaller than the normal size.

We can also write lg to make them bigger than normal.

Buttons

We can put spinners in buttons.

For example, we can write:

import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import Spinner from "react-bootstrap/Spinner";
import Button from "react-bootstrap/Button";

export default function App() {
  return (
    <>
      <Button variant="success" disabled>
        <Spinner as="span" animation="border" size="sm" />
        <span className="sr-only">Loading...</span>
      </Button>{" "}
      <Button variant="success" disabled>
        <Spinner as="span" animation="grow" size="sm" />
        Loading...
      </Button>
    </>
  );
}

to create buttons with the Spinner inside.

We make them small so they’re sized the same as the button text.

Tables

We can add tables with React Bootstrap.

It comes with the Table component to let us create simple tables:

import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import Table from "react-bootstrap/Table";

export default function App() {
  return (
    <>
      <Table striped bordered hover>
        <thead>
          <tr>
            <th>ID</th>
            <th>First Name</th>
            <th>Last Name</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>1</td>
            <td>james</td>
            <td>smith</td>
          </tr>
          <tr>
            <td>2</td>
            <td>mary</td>
            <td>jones</td>
          </tr>
          <tr>
            <td>3</td>
            <td colSpan="2">larry</td>
          </tr>
        </tbody>
      </Table>
    </>
  );
}

We just wrap the Table around the regular table elements to create a table.

striped makes the rows alternate in color.

bordered adds borders to the rows.

hover changes the color of the row when we highlight it.

Small Table

We can set the size prop to change the size of the table.

For instance, we can write:

import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import Table from "react-bootstrap/Table";

export default function App() {
  return (
    <>
      <Table striped bordered hover size="sm">
        <thead>
          <tr>
            <th>ID</th>
            <th>First Name</th>
            <th>Last Name</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>1</td>
            <td>james</td>
            <td>smith</td>
          </tr>
          <tr>
            <td>2</td>
            <td>mary</td>
            <td>jones</td>
          </tr>
          <tr>
            <td>3</td>
            <td colSpan="2">larry</td>
          </tr>
        </tbody>
      </Table>
    </>
  );
}

Then the rows are smaller than usual.

Dark Table

We can set the variant prop to dark to make it dark:

import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import Table from "react-bootstrap/Table";

export default function App() {
  return (
    <>
      <Table striped bordered hover variant="dark">
        <thead>
          <tr>
            <th>ID</th>
            <th>First Name</th>
            <th>Last Name</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>1</td>
            <td>james</td>
            <td>smith</td>
          </tr>
          <tr>
            <td>2</td>
            <td>mary</td>
            <td>jones</td>
          </tr>
          <tr>
            <td>3</td>
            <td colSpan="2">larry</td>
          </tr>
        </tbody>
      </Table>
    </>
  );
}

Now all the rows are dark.

Conclusion

We can add spinners to show that something is loading.

Tables can be added with the Table component.