Reactstrap Components In ReactJS - Part Three

Introduction

 
Reactstrap is a component library for Reactjs. It provides in-built Bootstrap components that provide flexibility and inbuilt validations, making it easy to create a UI. Reactstrap is similar to Bootstrap, but it has self-contained components.
 
You can check my previous articles in which we discussed how we add Reactstrap and basic Reactstrap components in Reactjs applications from the below links.
In this article we will discuss the following Reactstrap components,
  • Alerts
  • Badge
  • Spinners 
Prerequisites
  • We should have a basic knowledge of HTML and JavaScript.
  • Visual Studio Code be installed
  • Node and NPM installed
Let's create a new React project by using the following command,
  1. npx create-react-app reactstrapcomponent    
Install Reactstrap by using the following command, 
  1. npm install --save reactstrap react react-dom   
Now install Bootstrap in this project by using the following command.
  1. npm install --save bootstrap   
 Now, open the index.js file and add import Bootstrap.
  1. mport 'bootstrap/dist/css/bootstrap.min.css';   
 Now, in Visual Studio code, go to src folder and create a new folder and inside this folder add 3 new components, 
  • SpinnersDemo.js
  • AlertsDemo.js
  • BadgeDemo.js
Now open SpinnersDemo.js file and add the following code in this component, 
  1. import React, { Component } from 'react'  
  2. import { Navbar, Nav, NavItem, NavLink } from 'reactstrap';  
  3. import { Spinner } from 'reactstrap';  
  4. export class SpinnersDemo extends Component {  
  5.         render() {  
  6.                 return (  
  7.                         <div>  
  8.                                 <Navbar className="btn btn-info" light expand="md">  
  9.                                         <Nav color="info" navbar>  
  10.                                                 <NavItem className="hdr">  
  11.                                                         <NavLink style={{ "color""white" }} >Reactstrap Spinners Components</NavLink>  
  12.                                                 </NavItem>  
  13.                                         </Nav>  
  14.                                 </Navbar>  
  15.                                 <div style={{ "marginTop""10px" }}>  
  16.                                         <Spinner color="primary" />  
  17.                                         <Spinner color="info" />  
  18.                                         <Spinner color="light" />  
  19.                                 </div>  
  20.                                 <br></br>  
  21.                                 <div>  
  22.                                         <Spinner type="grow" color="secondary" />  
  23.                                         <Spinner type="grow" color="success" />  
  24.                                         <Spinner type="grow" color="info" />  
  25.                                 </div>  
  26.                         </div>  
  27.                 )  
  28.         }  
  29. }  
  30.   
  31. export default SpinnersDemo  
Now open App.js file and add the following code,
  1. import React from 'react';  
  2. import logo from './logo.svg';  
  3. import './App.css';  
  4. import SpinnersDemo from './SpinnersDemo'  
  5. function App() {  
  6.   return (  
  7.     <div className="App">  
  8.       <SpinnersDemo></SpinnersDemo>  
  9.     </div>  
  10.   );  
  11. }  
  12.   
  13. export default App;  
Run the project by using 'npm start' and check the result.
 
Reactstrap Components In ReactJS 
Now open BadgeDemo.js file and add the following code in this component
  1. import React, { Component } from 'react'  
  2. import { Badge, Button} from 'reactstrap';  
  3. import { Navbar, Nav, NavItem, NavLink } from 'reactstrap';  
  4. export class BadgeDemo extends Component {  
  5.         render() {  
  6.                 return (  
  7.                         <div>  
  8.                               <Navbar className="btn btn-info" light expand="md">  
  9.                                         <Nav color="info" navbar>  
  10.                                                 <NavItem className="hdr">  
  11.                                                         <NavLink style={{ "color""white" }} >Reactstrap Badge Components</NavLink>  
  12.                                                 </NavItem>  
  13.                                         </Nav>  
  14.                                 </Navbar>     
  15.                                 <div style={{ "marginTop""10px" }}>  
  16.                                 <h2>News <Badge color="secondary">New</Badge></h2>  
  17.                                 <Badge color="danger">Danger</Badge><br></br>  
  18.                                 <Button color="primary" outline>  
  19.                                 Update <Badge color="secondary">4</Badge>  
  20.                                 </Button>  
  21.                                 </div>  
  22.                         </div>  
  23.                 )  
  24.         }  
  25. }  
  26.   
  27. export default BadgeDemo  
Now open App.js file and add the following code,
  1. import React from 'react';  
  2. import logo from './logo.svg';  
  3. import './App.css';  
  4. import BadgeDemo from './BadgeDemo'  
  5. function App() {  
  6.   return (  
  7.     <div className="App">  
  8.       <BadgeDemo></BadgeDemo>  
  9.     </div>  
  10.   );  
  11. }  
  12.   
  13. export default App;  
Run the project by using 'npm start' and check the result,
 
Reactstrap Components In ReactJS
Now open AlertsDemo.js file and add the following code in this component.
  1. import React, { useState } from 'react';  
  2. import { Alert } from 'reactstrap';  
  3. import { Navbar, Nav, NavItem, NavLink } from 'reactstrap';  
  4. const AlertsDemo = (props) => {  
  5.         const [visible, setVisible] = useState(true);  
  6.   
  7.         const onDismiss = () => setVisible(false);  
  8.   
  9.         return (  
  10.                 <>  
  11.                         <Navbar className="btn btn-info" light expand="md">  
  12.                                 <Nav color="info" navbar>  
  13.                                         <NavItem className="hdr">  
  14.                                                 <NavLink style={{ "color""white" }} >Reactstrap Alert Components</NavLink>  
  15.                                         </NavItem>  
  16.                                 </Nav>  
  17.                         </Navbar>  
  18.                         <Alert color="warning" isOpen={visible} toggle={onDismiss}>  
  19.                                 Alert Message  
  20.                        </Alert>  
  21.                        <Alert color="success" isOpen={visible} toggle={onDismiss}>  
  22.                                 Alert Message  
  23.                        </Alert>  
  24.                 </>  
  25.         );  
  26. }  
  27.   
  28. export default AlertsDemo;  
Now open App.js file and add the following code,
  1. import React from 'react';  
  2. import logo from './logo.svg';  
  3. import './App.css';  
  4. import AlertsDemo from './AlertsDemo'  
  5. function App() {  
  6.   return (  
  7.     <div className="App">  
  8.       <AlertsDemo></AlertsDemo>  
  9.     </div>  
  10.   );  
  11. }  
  12.   
  13. export default App;  
 Run the project by using 'npm start' and check the result,
 
Reactstrap Components In ReactJS

Summary

 
In this article we learned how to use Alerts, Badge, and Spinners in Reactstrap components. Reactstrap is a component library for ReactJS.