React Confirm Alert Dialog
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
-
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined.
cause Attempting to use `confirmAlert` without correctly importing it as a named export.fixEnsure you are using `import { confirmAlert } from 'react-confirm-alert';` and not `import confirmAlert from 'react-confirm-alert';` or `require` in an ESM context. -
Dialog appears without any styling or looks unformatted.
cause The required CSS file for `react-confirm-alert` has not been imported or is not being processed by the build system.fixAdd `import 'react-confirm-alert/src/react-confirm-alert.css';` to your main application file or the component where you use `confirmAlert`. Verify your build tools are correctly configured to handle CSS imports. -
TypeError: Cannot read properties of undefined (reading 'call') or similar React DOM errors when using with React 17 or older.
cause Using `react-confirm-alert` version 3.x.x (which targets React 18) with an application running React 17 or an older version.fixDowngrade `react-confirm-alert` to version 2.x.x by running `npm install react-confirm-alert@^2.0.0 --save`.
Warnings
- breaking Version 3.x.x of react-confirm-alert introduces support for React 18. If your project uses React 17.x.x or older, you must continue using react-confirm-alert version 2.x.x to ensure compatibility.
- gotcha The default styles for react-confirm-alert are not automatically bundled with the component. You must explicitly import the CSS file for the dialog to render with proper styling.
- gotcha When using `customUI`, it is your responsibility to handle the `onClose` callback provided by the component to ensure the dialog can be dismissed programmatically. Failing to call `onClose()` from your custom buttons will prevent the dialog from closing.
- deprecated Older versions might not have `targetId` or `keyCodeForClose` props. Always consult the documentation corresponding to your installed version, especially when upgrading or downgrading.
Install
-
npm install react-confirm-alert -
yarn add react-confirm-alert -
pnpm add react-confirm-alert
Imports
- confirmAlert
const { confirmAlert } = require('react-confirm-alert');import { confirmAlert } from 'react-confirm-alert'; - CSS
import 'react-confirm-alert/src/react-confirm-alert.css';
- types
import type { Options } from 'react-confirm-alert';
Quickstart
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;