Hook - useConfirm

원종서·2021년 8월 25일
0

hook

목록 보기
6/11
import React, {useEffect, useRef, useState} from "react";
import ReactDOM from "react-dom";

import "./styles.css";

const useConfirm = (message = "", A, B) => {
    // validator
    if (typeof A !== "function" || typeof B !== "function") {
        return;
    }

    return () => {
        if (window.confirm(message)) {
            A();
        } else {
            B();
        }
    };
};

const App = () => {
    const a = () => console.log("a");
    const b = () => console.log("b");

    const confirmAB = useConfirm("a or b", a, b);

    return (
        <div className="App">
            <button onClick={confirmAB}>A? or B?</button>
        </div>
    );
};

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

0개의 댓글