简体   繁体   English

Next.js 500 Internal Server Error 在 404 页面内调用 useEffect Hook 后立即出现错误

[英]Next.js 500 Internal Server Error right after calling useEffect Hook inside 404 Page

I am calling the useEffect hook to call a setTimeout function inside it so as to redirect the user back to the home page after 3 seconds using useRouter hook from Next.js and calling the push method on it.我正在调用 useEffect 钩子来调用其中的 setTimeout 函数,以便在 3 秒后使用 Next.js 中的 useRouter 钩子将用户重定向回主页,并在其上调用 push 方法。 The page works fine when i dont call the useEffect hook inside the stateless functional component for 404 page and redirects me to the custom 404 page.当我不在 404 页面的无状态功能组件内调用 useEffect 钩子并将我重定向到自定义 404 页面时,该页面工作正常。 But, then i get 500 internal server error every time i call useEffect Hook.但是,每次我调用 useEffect Hook 时,我都会收到 500 个内部服务器错误。

Error错误

Terminal :next_router__WEBPACK_IMPORTED_MODULE_4___default(...) is not a function at PageNotFound (webpack-internal:///./pages/404.js:23:68) at processChild终端 :next_router__WEBPACK_IMPORTED_MODULE_4___default(...) 不是 PageNotFound (webpack-internal:///./pages/404.js:23:68) at processChild 的函数

import Box from "@material-ui/core/Box";
import Link from "next/link";
import styles from "../styles/Home.module.css";
import useEffect from "react";
import useRouter from "next/router";


const PageNotFound = () => {
  const router = useRouter();

  useEffect(() => {
    setTimeout(() => {
      router.push("/");
    }, 3000);
  }, []);

  return (
    <Box className="page-not-found">
      <h1>Ooops..</h1>
      <h2>That page cannot be found</h2>
      <p>
        Go back to the
        <Link href="/">
          <a className={styles.notFoundHomePageLink}>Homepage.</a>
        </Link>
      </p>
    </Box>
  );
};

export default PageNotFound;

` `

It seems like you imported useRouter the wrong way.似乎您以错误的方式导入了useRouter According to the docs it needs to be like this:根据文档,它需要是这样的:

import { useRouter } from 'next/router';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM