カスタムフック - Hashimoto-Noriaki/react-calendar GitHub Wiki

例:ログインユーザー情報のカスタムフック

// useLoginUser.ts
import { useContext } from "react";
import { LoginUserContext } from "../contexts/LoginUserContext";

export const useLoginUser = () => {
  const context = useContext(LoginUserContext);
  if (!context) {
    throw new Error("useLoginUser must be used within a LoginUserProvider");
  }
  return context;
};

これを使えば、毎回 useContext(LoginUserContext) と書かずに済みます。