@jsnooks/use-confirm

1.0.1 • Public • Published

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;

Dependencies (5)

Dev Dependencies (0)

    Package Sidebar

    Install

    npm i @jsnooks/use-confirm

    Weekly Downloads

    0

    Version

    1.0.1

    License

    MIT

    Unpacked Size

    2.18 kB

    Total Files

    4

    Last publish

    Collaborators

    • largopie