最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting 404 | Page Not found Nextjs - Stack Overflow

programmeradmin1浏览0评论

I created my page routing with react-router v5 and everything works well. If I click on the link, it takes me to the page, but when I reload the page I get a "404 | Page Not Found" error on the page.

import React, { useEffect, useState } from 'react'
import Home from './dexpages/Home'
import {
  BrowserRouter,
  Routes,
  Route
} from "react-router-dom";
import Dashboard from './dexpages/dashboard';
import Swapping from './dexpages/swapping'
import Send from './dexpages/send';
import Airdrops from './dexpages/airdrops'

function Main() {
  const [mounted, setMounted] = useState(false)
  useEffect(() => {
    if (typeof window !== "undefined") {
      setMounted(true)
    }
  }, [])

  return (
    <>
      {mounted &&
        <BrowserRouter>
          <div className="min-h-screen" style={{ overflowX: 'hidden' }}>
            <Routes>
              <Route path="/airdrops" element={<Airdrops />} />
              <Route path="/send" element={<Send />} />
              <Route path="/swap" element={<Swapping />} />
              <Route path="/dashboard" element={<Dashboard />} />
              <Route path="/" element={<Home />} />
            </Routes>
          </div>
        </BrowserRouter>
      }
    </>

  )
}

export default Main;

This is my Main Component where I create all the routing.

This is the error I'm getting when I reload the page

I created my page routing with react-router v5 and everything works well. If I click on the link, it takes me to the page, but when I reload the page I get a "404 | Page Not Found" error on the page.

import React, { useEffect, useState } from 'react'
import Home from './dexpages/Home'
import {
  BrowserRouter,
  Routes,
  Route
} from "react-router-dom";
import Dashboard from './dexpages/dashboard';
import Swapping from './dexpages/swapping'
import Send from './dexpages/send';
import Airdrops from './dexpages/airdrops'

function Main() {
  const [mounted, setMounted] = useState(false)
  useEffect(() => {
    if (typeof window !== "undefined") {
      setMounted(true)
    }
  }, [])

  return (
    <>
      {mounted &&
        <BrowserRouter>
          <div className="min-h-screen" style={{ overflowX: 'hidden' }}>
            <Routes>
              <Route path="/airdrops" element={<Airdrops />} />
              <Route path="/send" element={<Send />} />
              <Route path="/swap" element={<Swapping />} />
              <Route path="/dashboard" element={<Dashboard />} />
              <Route path="/" element={<Home />} />
            </Routes>
          </div>
        </BrowserRouter>
      }
    </>

  )
}

export default Main;

This is my Main Component where I create all the routing.

This is the error I'm getting when I reload the page

Share Improve this question asked Apr 12, 2022 at 10:32 Aliyu KamiluAliyu Kamilu 1131 gold badge1 silver badge6 bronze badges 1
  • possibly related: stackoverflow.com/q/54815348/11107541 – starball Commented May 1, 2023 at 8:06
Add a comment  | 

3 Answers 3

Reset to default 11

Don't use React Router with next.js.

Next.js has its own routing mechanism which is used for both client-side routing and server-side routing.

By using React Router, you're bypassing that with your own client-side routing so when you request the page directly (and depend on server-side routing) you get a 404 Not Found.

Next.js has a migration guide.

For me another instance of the app was running and I was trying to load the old instance.

Your paths need to be adjusted. For the home page it is fine to be routed to / however for the other pages there is no need for a backslash, remove the backslash from the Airdrops, Send, Swapping, and Dashboard paths respectively and you should be fine.

Try this below for each of the routes.

<Route path="airdrops" element={<Airdrops />} /> <Route path="send" element={<Send />} /> <Route path="swap" element={<Swapping />} /> <Route path="dashboard" element={<Dashboard />} /> <Route path="/" element={<Home />} />

发布评论

评论列表(0)

  1. 暂无评论