reactstrap

Easy to use React Bootstrap 4 components

View on GithubView Components

Installation


NPM

Install reactstrap and peer dependencies via NPM

npm install --save reactstrap@next react react-dom

Import the components you need

import React from 'react';
import { Button } from 'reactstrap';

export default (props) => {
  return (
    <Button color="danger">Danger!</Button>
  );
};

Getting Started with Create React App

Follow the create-react-app instructions up to the Adding Bootstrap section and instead follow the reactstrap version of adding bootstrap.

tl;dr

npm install -g create-react-app

create-react-app my-app
cd my-app/
npm start

Then open http://localhost:3000/ to see your app. The initial structure of your app is setup. Next, let's add reactstrap and bootstrap.

Adding Bootstrap

Install reactstrap and Bootstrap from NPM. Reactstrap does not include Bootstrap CSS so this needs to be installed as well:

npm install bootstrap@4.0.0-beta.2 --save
npm install --save reactstrap@next react react-dom

Import Bootstrap CSS in the src/index.js file:

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

Import required reactstrap components within src/App.js file or your custom component files:

import { Button } from 'reactstrap';

Now you are ready to use the imported reactstrap components within your component hierarchy defined in the render method. Here is an example App.js redone using reactstrap.

CDN

Reactstrap can be included directly in your application's bundle or excluded during compilation and linked directly to a CDN.

https://cdnjs.cloudflare.com/ajax/libs/reactstrap/4.8.0/reactstrap.min.js

Note: When using the external CDN library, be sure to include the required dependencies as necessary prior to the Reactstrap library:

Check out the demo here

About the Project


This library contains React Bootstrap 4 components that favor composition and control. The library does not depend on jQuery or Bootstrap javascript. However, https://popper.js.org/ via https://github.com/souporserious/react-popper is relied upon for advanced positioning of content like Tooltips, Popovers, and auto-flipping Dropdowns.

There are a few core concepts to understand in order to make the most out of this library.

1) Your content is expected to be composed via props.children rather than using named props to pass in Components.

// Content passed in via props
const Example = (props) => {
  return (
    <p>This is a tooltip <TooltipTrigger tooltip={TooltipContent}>example</TooltipTrigger>!</p>
  );
}

// Content passed in as children (Preferred)
const PreferredExample = (props) => {
  return (
    <p>
      This is a <a href="#" id="TooltipExample">tooltip</a> example.
      <Tooltip target="TooltipExample">
        <TooltipContent/>
      </Tooltip>
    </p>
  );
}

2) Attributes in this library are used to pass in state, conveniently apply modifier classes, enable advanced functionality (like popperjs), or automatically include non-content based elements.

Examples:

  • isOpen - current state for items like dropdown, popover, tooltip
  • toggle - callback for toggling isOpen in the controlling component
  • color - applies color classes, ex: <Button color="danger"/>
  • size for controlling size classes. ex: <Button size="sm"/>
  • tag - customize component output by passing in an element name or Component
  • boolean based props (attributes) when possible for alternative style classes or sr-only content