Reactstrap Components In ReactJS - Part Two

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 in Reactjs application from the below links.
In this article we will discuss the following Reactstrap components,
  • Modals
  • Toasts
  • Tooltip
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. import '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,
  1. Modals.js
  2. Toasts.js
  3. Tooltips.js
 Modals is used to  display a message or information in a popup window on click or focus event. Now open Modals.js file and add the following code in this component,
  1. import React, { useState } from 'react';  
  2. import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from 'reactstrap';  
  3. import { Navbar, Nav, NavItem, NavLink } from 'reactstrap';  
  4.   
  5. const Models = (props) => {  
  6.         const {  
  7.                 buttonLabel,  
  8.                 className  
  9.         } = props;  
  10.   
  11.         const [modal, setModal] = useState(false);  
  12.   
  13.         const toggle = () => setModal(!modal);  
  14.   
  15.         return (  
  16.                 <div>  
  17.                         <Navbar className="btn btn-warning" light expand="md">  
  18.                                 <Nav color="info" navbar>  
  19.                                         <NavItem className="hdr">  
  20.                                                 <NavLink>Reactstrap Modals Components</NavLink>  
  21.                                         </NavItem>  
  22.                                 </Nav>  
  23.                         </Navbar>  
  24.                         <Button color="info" onClick={toggle}>Open Modal</Button>  
  25.                         <Modal isOpen={modal} toggle={toggle} className={className}>  
  26.                                 <ModalHeader style={{ 'backgroundColor'"#0062cc" }} color="primary" toggle={toggle}>Modal title</ModalHeader>  
  27.                                 <ModalBody>  
  28.                                         Bengaluru (also called Bangalore) is the capital of India's southern Karnataka state. The center of India's high-tech industry, the city is also known for its parks and nightlife. By Cubbon Park, Vidhana Soudha is a Neo-Dravidian legislative building. Former royal residences include 19th-century Bangalore Palace, modeled after England’s Windsor Castle, and Tipu Sultan’s Summer Palace, an 18th-century teak structure.  
  29.               </ModalBody>  
  30.                                 <ModalFooter>  
  31.                                         <Button color="primary">Save</Button>  
  32.                                         <Button className="btn btn-danger">Cancel</Button>  
  33.                                 </ModalFooter>  
  34.                         </Modal>  
  35.                 </div>  
  36.         );  
  37. }  
  38.   
  39. export default Models;  
Now open app.js file and the following code in this file,
  1. import React from 'react';  
  2. import logo from './logo.svg';  
  3. import './App.css';  
  4. import Models from './Models'  
  5. function App() {  
  6.   return (  
  7.     <div className="App">  
  8.       <Models></Models>  
  9.     </div>  
  10.   );  
  11. }  
  12.   
  13. export default App;  
Now run the project and check the result. 
 
Reactstrap Components In ReactJS
 

Tooltips

 
Tooltip is a pop-up that displays a message or information on hover, click or focus event. Let's open Tooltips.js file and add the following code in this component.
  1. import React, { useState } from 'react';  
  2. import { Tooltip, Button } from 'reactstrap';  
  3. import { Navbar, Nav, NavItem, NavLink } from 'reactstrap';  
  4. const Tooltipsdemo = (props) => {  
  5.         const [tooltipOpen, setTooltipOpen] = useState(false);  
  6.   
  7.         const toggle = () => setTooltipOpen(!tooltipOpen);  
  8.   
  9.         return (  
  10.                 <div>  
  11.                         <Navbar className="btn btn-warning" light expand="md">  
  12.                                 <Nav color="info" navbar>  
  13.                                         <NavItem className="hdr">  
  14.                                                 <NavLink>Reactstrap Tooltips Components</NavLink>  
  15.                                         </NavItem>  
  16.                                 </Nav>  
  17.                         </Navbar>  
  18.   
  19.                         <Button color="primary"><span href="#" id="btntip">Button</span></Button>  
  20.                         <Tooltip placement="top" isOpen={tooltipOpen} autohide={false} target="btntip" toggle={toggle}>  
  21.                                 Tooltip Demo  
  22.             </Tooltip>  
  23.                 </div>  
  24.         );  
  25. }  
  26.   
  27. export default Tooltipsdemo;  
Now open app.js file and the following code in this file,
  1. import React from 'react';  
  2. import logo from './logo.svg';  
  3. import './App.css';  
  4. import Tooltipsdemo from './Tooltipsdemo'  
  5. function App() {  
  6.   return (  
  7.     <div className="App">  
  8.       <Tooltipsdemo></Tooltipsdemo>  
  9.     </div>  
  10.   );  
  11. }  
  12.   
  13. export default App;  
Now run the project and check the result.
 
Reactstrap Components In ReactJS
 

Toastr

 
Toastr is used to create a notification popup. Let's open ToastDemo.js file and add the following code in this component.
  1. import React, { useState } from 'react';  
  2. import { Button, Toast, ToastBody, ToastHeader } from 'reactstrap';  
  3. import { Navbar, Nav, NavItem, NavLink } from 'reactstrap';  
  4. const Toastdemo = (props) => {  
  5.         const [show, setShow] = useState(false);  
  6.   
  7.         const toggle = () => setShow(!show);  
  8.   
  9.         return (  
  10.                 <div>  
  11.                         <Navbar className="btn btn-warning" light expand="md">  
  12.                                 <Nav color="info" navbar>  
  13.                                         <NavItem className="hdr">  
  14.                                                 <NavLink>Reactstrap Toasts Components</NavLink>  
  15.                                         </NavItem>  
  16.                                 </Nav>  
  17.                         </Navbar>  
  18.                         <Button color="primary" onClick={toggle}>Toastr</Button>  
  19.                         <br />  
  20.                         <br />  
  21.                         <Toast isOpen={show}>  
  22.                                 <ToastHeader toggle={toggle}>Toast title</ToastHeader>  
  23.                                 <ToastBody>  
  24.                                         Toastr Demo  
  25.         </ToastBody>  
  26.                         </Toast>  
  27.                 </div>  
  28.         );  
  29. }  
  30.   
  31. export default Toastdemo;  
Now run the project and check the result.
 
Reactstrap Components In ReactJS

Summary

 
In this article we learned how to use Modals Tooltips, and Toast components. Reactstrap is a component library for ReactJS.