Example 2.2
Wed May 15 2024 10:33:47 GMT+0000 (Coordinated Universal Time)
Saved by
@beyza
import React, { useState } from 'react';
import ReactDOM from 'react-dom';
const Modal = ({ isOpen, onClose, children }) => {
if (!isOpen) return null;
return ReactDOM.createPortal(
<div className="modal-overlay">
<div className="modal">
<button className="close-button" onClick={onClose}>Close</button>
{children}
</div>
</div>,
document.getElementById('modal-root')
);
};
const App = () => {
const [isModalOpen, setIsModalOpen] = useState(false);
const openModal = () => {
setIsModalOpen(true);
};
const closeModal = () => {
setIsModalOpen(false);
};
return (
<div>
<button onClick={openModal}>Open Modal</button>
<Modal isOpen={isModalOpen} onClose={closeModal}>
<h2>Modal Content</h2>
<p>This is a simple modal</p>
</Modal>
</div>
);
};
ReactDOM.render(<App />, document.getElementById('root'));
content_copyCOPY
https://chatgpt.com/c/be637bfa-4b91-4dd6-aedf-0664dd65725c
Comments