React Confirm Alert Dialog

3.0.6 · active · verified Sun Apr 19

react-confirm-alert is a React component that provides a customizable confirmation dialog, enabling developers to prompt users for actions like 'Yes/No' or 'Confirm/Cancel'. The current stable version is 3.0.6, which specifically supports React 18. For projects using React 17.x.x or older, version 2.x.x should be used. This library offers a straightforward functional API and also allows for fully custom UI components, providing significant flexibility over native browser `confirm()` dialogs. Its release cadence appears moderate, with several minor updates addressing features and bug fixes within the 2.x.x and 3.x.x series. A key differentiator is its simplicity and built-in styling, coupled with strong customization options for advanced use cases.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to import and use `confirmAlert` with a basic 'Yes/No' button configuration. It includes the necessary CSS import and shows how to attach the dialog to a button click handler, providing a title, message, and action callbacks.

import React from 'react';
import { confirmAlert } from 'react-confirm-alert'; // Import
import 'react-confirm-alert/src/react-confirm-alert.css'; // Import css

const App = () => {
  const handleSubmit = () => {
    confirmAlert({
      title: 'Confirm to submit',
      message: 'Are you sure you want to proceed with this action?',
      buttons: [
        {
          label: 'Yes',
          onClick: () => alert('Action confirmed!')
        },
        {
          label: 'No',
          onClick: () => console.log('Action cancelled.')
        }
      ],
      closeOnEscape: true,
      closeOnClickOutside: true,
      overlayClassName: "custom-overlay"
    });
  };

  return (
    <div className='container' style={{ padding: '20px' }}>
      <h1>React Confirm Alert Demo</h1>
      <button onClick={handleSubmit} style={{ padding: '10px 20px', fontSize: '16px', cursor: 'pointer' }}>
        Show Confirm Dialog
      </button>
      <p>Click the button to open a confirmation dialog.</p>
    </div>
  );
};

export default App;

view raw JSON →