Important Update
The Guide Feature will be discontinued after December 15th, 2023. Until then, you can continue to access and refer to the existing guides.
Author avatar

Gaurav Singhal

How to Make a Modal Popup Refresh Items on the Page

Gaurav Singhal

  • Jul 14, 2020
  • 6 Min read
  • 155,697 Views
  • Jul 14, 2020
  • 6 Min read
  • 155,697 Views
Web Development
Front End Web Development
Client-side Framework
React

Introduction

Modal popups are a great way to manage content on the page and attract attention to specific features of an app. It's important to understand how to manipulate a React Bootstrap modal and customize it to cater to the needs of your app. For instance, you might want to reload the page after closing a review form popup on a website that shows user-based movie ratings and reviews. An API call fetching all the reviews can be put under a function that fires when the page first loads or mounts on the DOM. This way, the new review can be shown to the user right after they add it via the form.

In this guide, you'll learn several methods to refresh a page on closing a react-bootstrap modal.

Creating a React Bootstrap Modal

To get started, install react-bootstrap and bootstrap by running the following command:

1npm install react-bootstrap bootstrap
shell

Render the modal component inside App.js using the following code:

1import React,{useState,useEffect} from 'react';
2import {Modal} from 'react-bootstrap';
3import {Button} from 'react-bootstrap';
4import 'bootstrap/dist/css/bootstrap.min.css';
5import './App.css';
6
7function App() {
8
9   const [show, setShow] = useState(false);
10
11  const handleClose = () => setShow(false);
12  const handleShow = () => setShow(true);
13  
14  return (
15    <>
16      <Button variant="primary" onClick={handleShow}>
17        Launch demo modal
18      </Button>
19
20      <Modal show={show} onHide={handleClose}>
21        <Modal.Header closeButton>
22          <Modal.Title>Modal heading</Modal.Title>
23        </Modal.Header>
24        <Modal.Body>Woohoo, you're reading this text in a modal!</Modal.Body>
25        <Modal.Footer>
26          <Button variant="secondary" onClick={handleClose}>
27            Close
28          </Button>
29          <Button variant="primary" onClick={handleClose}>
30            Save Changes
31          </Button>
32        </Modal.Footer>
33      </Modal>
34    </>
35  );
36}
37
38export default App;
jsx

Checking Page Reload Using useEffect()

To check page reloads, you can use componentDidMount() or its equivalent hooks implementation of useEffect(). You can put relevant console.log() or alerts inside it that notify you of page reloads.

In React, this is done by passing an empty array as the second parameter to the useEffect() hook.

1useEffect(()=>{
2    alert('reload!')
3},[])
jsx

Every time your component mounts on the DOM or first loads on the page, you will get the above alert.

Tracking Modal's Exit Events Using Props

By default, the modal animates in and out of the page. There are several events that are triggered when a modal is closed. Each event is triggered after a specific phase change that the modal undergoes. This phase change is usually a transition stage amid its entire animation. These stages can be accessed by passing specific props to the modal and invoking a callback function.

You get access to three props: onExit, onExited, and onExiting. When the modal begins to transition out of the page, first the onExiting() callback is fired followed by the onExited() callback when the modal finishes transitioning out. Finally, the onExit() callback function is fired, indicating that the modal has completely transitioned out of the page.

Another prop is onHide, which fires a callback function when you click on the modal's close icon or click anywhere in the background outside the modal.

The below line of code shows how you can pass your own callback function as props to React Bootstrap's modal component:

1<Modal show={show} onHide={handleClose} onHide={callback}>
jsx

Refreshing Page Inside Callbacks

Create a simple function inside the App component or any parent component rendering your modal.

1 const reload=()=>window.location.reload();
jsx

The above reload() method forcefully reloads the page by invoking the built-in reload method on the browser's global object (the window object) property, the location object.

Pass this method to any of the exit events discussed in the previous section. For example, use the onExit event:

1<Modal show={show} onHide={handleClose} onExit={reload}>
jsx

You can use the onExited or onExiting's events to get the same result:

1<Modal show={show} onHide={handleClose} onExiting={reload}> 
jsx

Alternately, you can also use the onHide prop to produce the same effect.

1<Modal show={show} onHide={handleClose} onHide={reload}>
jsx

Using any of the above methods,. you get a full page to reload and an alert as the validation on closing the modal.

Refreshing Page Using onClick Event

In case you do not want to pass any additional props to your modal component, you can call the custom reload() method inside handleClose(). This is a mandatory function required to close the modal invoked by the onHide event. Call the reload() method after setting the modal's show state to false.

1  const handleClose = () => {
2    setShow(false)
3    reload();
4  };
jsx

This is similar to firing a callback after the onHide event but offers a cleaner way to achieve the same result.

Remember to call these methods for onClick events on UI elements inside the modal that are meant to close it, such as the close button in the modal's footer.

1<Button variant="secondary" onClick={handleClose}>
2    Close
3</Button>
jsx

Conclusion

Page reloads do not offer a good user experience. They must always be coupled with necessary actions such as retrieving or sending data to the server or loading heavy assets like large images in the background. To combat this bad user experience, page reloads must always be accompanied by spinners or loaders. All the methods demonstrated in this guide work equally fine, but in situations where some props can not be accessed (for instance, when removing transitions or animations from the modal) you must use custom event handlers.

Learn More

Explore these React and Bootstrap course from Pluralsight to continue learning: