@jsnooks/use-confirm
v1.0.1
Published
React Hook to prompt a confirmation before executing a function
Downloads
1
Maintainers
Readme
useConfirm
- 확인 버튼을 누를 시
onConfirm
, 취소 버튼을 누를 시onCancel
을 반환하는 함수형 프로그램
[실습예제]
import React from "react";
const useConfirm = (message = "", onConfirm, onCancel) => {
if (!onConfirm && typeof onConfirm !== "function") {
return;
}
if (!onCancel && typeof onCancel !== "function") {
return;
}
const confirmAction = () => {
if (confirm(message)) {
onConfirm();
} else {
onCancel();
}
};
return confirmAction;
};
const App = () => {
const deleteWorld = () => console.log("Deleting the world...");
const abort = () => console.log("Aborted");
const confirmDelete = useConfirm("Are you sure?", deleteWorld, abort);
return (
<div className="App">
<button onClick={confirmDelete}>Delete the World</button>
</div>
);
};
export default App;