import React, { useEffect, useState } from "react"; import type { GetStaticPropsContext } from "next"; import { useRouter } from "../services/Router"; export function withStaticProps( getStaticProps: (context: GetStaticPropsContext) => Promise, ErrorComponent: React.ComponentType<{ statusCode?: number }> ) { return (Component: React.ComponentType) => () => { const router = useRouter(); const [staticProps, setStaticProps] = useState(null); const [invalidState, setInvalidState] = useState< "error" | "notFound" | null >(null); useEffect(() => { async function fetchStaticProps() { try { setInvalidState(null); const res = await getStaticProps({ params: router.query }); if (res.notFound) { setInvalidState("notFound"); return; } setStaticProps(res.props); } catch (err) { setInvalidState("error"); } } fetchStaticProps(); }, [router.query]); if (!staticProps) { // optional return null; } if (invalidState === "notFound") { return ; } if (invalidState === "error") { return ; } return ; }; }