Notice
Recent Posts
Recent Comments
Link
250x250
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- npm start
- 글자들여쓰기
- legacy-peer
- react npm install
- 정적객체
- MediaQuery
- 웹아이콘
- node설치
- npm install 문제
- minwidth
- fontawesome
- window 정책변경
- owlcarousel
- slickslider
- Git clone
- node 오류
- npm install
- 플러그인
- XEIcon
- 단어단위로떨어지기
- 아이콘사용법
- 이미지반응형
- googleicon
- 그누보드반응형
- git lab clone
- 의존성문제
- vscode git clone
- package.json
- 동적객체
- maxwidth
Archives
- Today
- Total
어쩌다 알게 된 ƪ(•̃͡•̃͡ ƪ
🟦 [React] 모달 창 띄우기 본문
728x90
처음 만든 모달
Modal.jsx
import './Modal.css'
const Modal = ({ isOpen, onClose, title, content, onConfirm, onCancle, width, height }) => {
if(!isOpen) return null;
return (
<div className="modal">
<div className="modal_inner">
<div className="modal_content" style={{ width, height }}>
<div className="modal_header">
<span className="close" onClick={onClose}>×</span>
<div className="modal_title">{title}</div>
</div>
<div className="modal_body">{content}</div>
<div className="modal_footer">
{
onConfirm ?
<button className='btn btn_style1 modal_confirm neg' onClick={{onCancle}}>취소</button>
:
<button className='btn btn_style1 modal_confirm pos' onClick={{onConfirm}}>확인</button>
}
</div>
</div>
</div>
</div>
)
}
export default Modal;
App.jsx
import { useState } from 'react'
import Modal from './Modal';
function App() {
const [isModalOpen, setIsModalOpen] = useState(false);
const handleOpenModal = () => {
setIsModalOpen(true);
};
const handleCloseModal = () => {
setIsModalOpen(false);
};
return (
<>
<div>
<h1>React Modal</h1>
<button onClick={handleOpenModal}>버튼</button>
<Modal
isOpen={isModalOpen}
onClose={handleCloseModal}
title="타이틀"
content={<div className="modal_inquiry_result">등록 완료 되었습니다.</div>}
onConfirm={handleCloseModal}
onCancle={handleCloseModal}
width="400px"
height="initial"
/>
</div>
</>
)
}
export default App
하나의 팝업만 뜰 땐 괜찮으나 실제로는 같은 페이지 내에서도 수십개씩 띄워야 하기 때문에
어떻게 코드를 줄일까 생각하고 다시 짠 코드
[React] 모달 창 띄우기2_코드 재사용화, 컴포넌트화 시키기
Modal.jsx 👇import './Modal.css'const Modal = ({ isOpen, onClose, title, content, onBtn = [], onCancle, width, height }) => { if(!isOpen) return null; return ( × {title} {content} {onBtn.map((btn, index) => ( {btn.text} )) } )}expor..
dazzle-bini.tistory.com
728x90
'개발 > 🟦 React' 카테고리의 다른 글
| 🟦 [React] onclick={함수()}, onclick={() => 함수()} 차이점 (0) | 2025.03.18 |
|---|---|
| 🟦 [React] 모달 창 띄우기2_코드 재사용화, 컴포넌트화 시키기 (0) | 2025.03.17 |
| 🟦 [React] npm, npx 차이 (0) | 2025.03.17 |
| 🟦 [React] Next.js란? (0) | 2025.03.17 |
| 🟦 [React] Chart.js 라이브러리 설치 o (0) | 2025.03.09 |
Comments
