1.0.1 • Published 2 years ago

@jsnooks/use-confirm v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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;