We can see what to expect after triggering modals for React Bootstrap down below for a better idea of the task at hand.
Triggering modals for React Bootstrap can be a tricky task if not done in the correct manner. We can achieve this within a React application by following the steps listed below.
If we don’t have a basic React App already present on our systems, we can create one using the following commands in the terminal one after the other.
npx create-react-app <name-of-directory>
cd <name-of-directory>
Note: We can write a directory name of our choice in place of the
<name-of-directory>
tag in the commands above.
Next, we install the react-bootstrap
and bootstrap
packages with the command shown below.
npm install react-bootstrap bootstrap
Inside the src
folder of our React App, create a new file named ModalComponent.js
that will contain our modal component. A sample code of ModalComponent.js
is shown below that can be modified to fulfil our specific requirements.
// src/ModalComponent.js file//importing the necessary components and functions from React and React Bootstrapimport React, { useState } from 'react';import { Button, Modal } from 'react-bootstrap';const ModalComponent = () => {//creating variables 'show' and 'setshow' to control the modal's visibilityconst [show, setShow] = useState(false);//the handleClose function hides the modal when calledconst handleClose = () => setShow(false);//the handleShow function shows the modal when calledconst handleShow = () => setShow(true);return (<>{/* Create a button that, when clicked, calls the 'handleShow' function to open the modal. */}<Button variant="primary" onClick={handleShow}>Open Modal</Button>{/*Modal component is defined. The 'show' prop controls its visibility, and 'onHide' is called when it's closed. */}<Modal show={show} onHide={handleClose}><Modal.Header closeButton>{/* Define the title for the modal. */}<Modal.Title>Modal Title</Modal.Title></Modal.Header><Modal.Body>{/* Define the content of the modal. This section of the code can be modified to our preference */}This is the modal content. You can put any JSX here.</Modal.Body><Modal.Footer>{/*A 'Close' button is created that calls 'handleClose' to close the modal. */}<Button variant="secondary" onClick={handleClose}>Close</Button>{/* A 'Save Changes' button is created that also calls 'handleClose' to close the modal. */}<Button variant="primary" onClick={handleClose}>Save Changes</Button></Modal.Footer></Modal></>);};export default ModalComponent;
By navigating to the src/App.js
file, import and use the modal component that was used in the previous step. This can be done as shown below for better understanding.
// src/App.js fileimport React from 'react';import './App.css';import ModalComponent from './ModalComponent';function App() {/*creating a <div> element with the class "App" for styling purposes.This is done inside the main render function for the App component */return (<div className="App"><header className="App-header"><ModalComponent /></header></div>);}export default App;
Note: Make sure the method and file names
ModalComponent
are written correctly when importing it in theApp.js
file. We can modify these names to whatever we please, but we must use consistent naming to avoid any import errors.
For styling, Bootstrap requires some CSS. We must ensure that we import the Bootstrap CSS by navigating to the src/index.js
file. The code to implement this step is given below.
// src/index.js file//making necessary module important from different files and React and ReactDOMimport React from 'react';import ReactDOM from 'react-dom';import './index.css';import App from './App';// Importing Bootstrap CSS, allowing us to use Bootstrap styles in our applicationimport 'bootstrap/dist/css/bootstrap.min.css';//Rendering the application using ReactDOMReactDOM.render(<React.StrictMode><App /></React.StrictMode>,document.getElementById('root'));
Finally, we run the React App we just created with the modifications described above, via the command shown below.
npm start
After implementing all of the steps above, we can finally see how the output of triggering modals in React Bootstrap will look like in a React application. Run it to see it in action!
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <link rel="icon" href="%PUBLIC_URL%/favicon.ico" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="theme-color" content="#000000" /> <meta name="description" content="Web site created using create-react-app" /> <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" /> <!-- manifest.json provides metadata used when your web app is installed on a user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/ --> <link rel="manifest" href="%PUBLIC_URL%/manifest.json" /> <!-- Notice the use of %PUBLIC_URL% in the tags above. It will be replaced with the URL of the `public` folder during the build. Only files inside the `public` folder can be referenced from the HTML. Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will work correctly both with client-side routing and a non-root public URL. Learn how to configure a non-root public URL by running `npm run build`. --> <title>React App</title> </head> <body> <noscript>You need to enable JavaScript to run this app.</noscript> <div id="root"></div> <!-- This HTML file is a template. If you open it directly in the browser, you will see an empty page. You can add webfonts, meta tags, or analytics to this file. The build step will place the bundled scripts into the <body> tag. To begin the development, run `npm start` or `yarn start`. To create a production bundle, use `npm run build` or `yarn build`. --> </body> </html>
Note: After launching the React App, we will see a “Open Modal” button at the top of the webpage. By clicking this, the
handleShow
function (inModalComponent.js
) is triggered, causing the modal to be displayed on the screen.
Overall, modals in React Bootstrap go a long way in enhancing user experience by maximizing the use of available screen space. This is done by overlaying content on top of the current webpage that proves to be beneficial for displaying additional information, without cluttering the main content area. Other benefits include a more responsive design and easy customisation, which make modals a handy feature for web developers to use in their everyday development.
Free Resources