React.js Custom Modal Example & Tutorial

React.js Custom Modal Example & Tutorial

React.js Custom Modal Example & Tutorial - In this article, I am going to create a demo for React.js custom modal. For this, I will use here react-modal NPM package...

React.js Custom Modal Example & Tutorial - In this article, I am going to create a demo for React.js custom modal. For this, I will use here react-modal NPM package...

There are so many other third-party libraries available today, Here I am going to use one of the very popular libraries.

Let's Get Started

Step 1: Create React App

create a very basic app of react.js by using below command over terminal.

create-react-app reactjs-custom-modal

Step 2: Create Modal Component

Create a folder inside src/ folder named modal. Now create two files inside this first modal.js and second, modal.css. Now open modal.js file and add below code in it.

import React from 'react';

import './modal.css';

const modal = (props) => {
return (
<div className="modal-wrapper"
transform: ? 'translateY(0vh)' : 'translateY(-100vh)',
opacity: ? '1' : '0'
<div className="modal-header">
<h2>Modal Header</h2>
<span className="close-modal-btn" onClick={props.close}>×</span>
<div className="modal-body">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
<div className="modal-footer">
<p>This is Modal footer</p>

export default modal;

Above, Modal component is a functional component which is receiving the contents as the children of the component. We are having 3 props for showing the modal, closing the modal and children for contents of the modal. Some CSS class has been added in HTML elements whose definition is written inside modal.css which I am going to explain just after it.

Now open the *modal.css *file and put below code inside it.

.modal-wrapper {
background: white;
border: 1px solid #d0cccc;
box-shadow: 0 5px 8px 0 rgba(0,0,0,0.2), 0 7px 20px 0 rgba(0,0,0,0.17);
margin: 100px auto 0;
transition: all .8s;
width: 60%;
border-radius: 10px;

.modal-header {
background: #e2e2e2;
height: 40px;
line-height: 40px;
padding: 5px 20px;
text-align: right;

.modal-header h2 {
float: left;
margin: 0;
padding: 0;

.modal-body {
padding: 10px 15px;
text-align: center;

.modal-footer {
background: #e2e2e2;
height: 35px;
padding: 15px;

.close-modal-btn {
color: white;
cursor: pointer;
float: right;
font-size: 30px;
margin: 0;

.close-modal-btn:hover {
color: black;

.btn-cancel {
background-color: #b71c1c;
float: left;

.btn-continue {
background-color: #1b5e20;
float: right;

.back-shed {
background-color: rgba(48, 49, 48, 0.42);
height: 100%;
position: fixed;
transition: all 1.3s;
width: 100%;

.open-modal-btn {
margin: 15px;
padding: 10px;

Step 3: Update App.js

import React, { Component } from 'react';
import Modal from './modal/modal';
import './App.css';

class App extends Component {

  constructor() {

    this.state = {
      isShowing: false

  openModalHandler = () => {
      isShowing: true

  closeModalHandler = () => {
      isShowing: false

  render () {
    return (

        { this.state.isShowing ? <div onClick={this.closeModalHandler} className="back-shed"></div> : null }

        <button className="open-modal-btn" onClick={this.openModalHandler}>Open Modal</button>


export default App;

In the above code, At the top, we have imported the modal component and used in the render method with the values of the props passed to it. After that we can see two custom methods:

*1. openModalHandler(): *It sets the state of isShowing to true to open the modal.

2. *closeModalHandler(): *It sets the state of isShowing to false to close the modal.

After that, An onClick listener is passed to the div to controll the opening and closing the modal.

Step 4: Run the app

After all the previous steps, Run the app over terminal with npm start. A new tab will open with url http://locahost. A screen like below will open.


So in this demo, We learn to create custom modal in React.js with very simple steps. You can find other demos of React.js Sample Projects here

That’s all for now. Thank you for reading and I hope this demo will be very helpful to create React.js custom modal.

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

ReactJS vs Angular vs Vue: Best Javascript Framework For Your Project

ReactJS vs Angular vs Vue: Best Javascript Framework For Your Project. This video covers the key differences between ReactJS, Angular and Vue with respect to the following: Use case, Performance, Data binding, Scripting language, Testing, Community support, Growth curve

What is JavaScript – All You Need To Know About JavaScript

In this article on what is JavaScript, we will learn the basic concepts of JavaScript.

JavaScript Tutorial: if-else Statement in JavaScript

This JavaScript tutorial is a step by step guide on JavaScript If Else Statements. Learn how to use If Else in javascript and also JavaScript If Else Statements. if-else Statement in JavaScript. JavaScript's conditional statements: if; if-else; nested-if; if-else-if. These statements allow you to control the flow of your program's execution based upon conditions known only during run time.