const useConfirm = (message = "", onConfirm, onCancle) => {
if (!onConfirm || typeof onConfirm !== "function") {
return;
}
if (onCancle && typeof onCancle !== "function") {
return;
}
const confirmAction = () => {
if (confirm(message)) {
onConfirm();
} else {
onCancle();
}
};
return confirmAction;
};
const App = () => {
const del = () => console.log("Deleting...")
const cancle = () => console.log("Cancled..")
const confirmDelete = useConfirm("Are you sure?", del, cancle);
return (
<div>
<button onClick={confirmDelete}>Delete</button>
</div>
);
};
*참고 강의: 노마드코더 > 실전형 리액트 Hooks