You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React单页应用锚点链接本地正常但部署后404及react-router-dom路由配置异常问题求助

Let’s break this down first. When you run your app locally with npm start, the Webpack Dev Server is pre-configured to route all requests through your index.html file. So even when you click /App#story, it loads your React app first, which then handles the anchor scroll to the #story element.

But production servers (like Nginx, Apache, or static hosts) work differently by default: they look for actual files matching the URL path. When you click /App#story, the server ignores the anchor part initially and tries to find a file at /App—since no such file exists, it throws a 404.

Quick Fixes:

  • Use React Router’s anchor handling: Swap plain <a> tags for Link components from react-router-dom, then add logic to scroll to the target element on route load:
    <Link to="/#story" className="font-medium text-gray-100 hover:text-red-600">Story</Link>
    
    Add this useEffect hook in your App component to handle scrolling:
    useEffect(() => {
      const hash = window.location.hash;
      if (hash) {
        const element = document.getElementById(hash.slice(1));
        element?.scrollIntoView({ behavior: 'smooth' });
      }
    }, [window.location.hash]);
    
  • Configure your production server: Set up your server to redirect all requests to index.html so React can handle routing. For example, in Nginx add:
    location / {
      try_files $uri $uri/ /index.html;
    }
    
  • Try HashRouter: If server config isn’t an option, switch from BrowserRouter to HashRouter. This adds a # to your routes (e.g., /#/App), and servers ignore everything after the #, so they’ll always serve index.html.

2. Fixing React Router’s "Content Append + Full Reload" Problem

Your issue here comes from two mistakes:

  1. Using plain <a> tags instead of Link components—this forces a full browser reload instead of using React’s client-side routing.
  2. Your main app content (menu, story section, etc.) isn’t wrapped in a Route, so it renders on every route. When you navigate to /about, it loads the original app content plus the About component below it.

Step-by-Step Fix:

  1. Replace <a> tags with Link:
    // Instead of this:
    <a href="/about" class="text-base text-gray-300 hover:text-white"> About </a>
    
    // Use this:
    <Link to="/about" className="text-base text-gray-300 hover:text-white"> About </Link>
    
  2. Restructure your Routes properly: Wrap your main page content in a Route with the exact prop, so it only renders when the path is exactly /. Keep your navigation outside the Switch so it’s always visible:
    function App() {
      return (
        <Router>
          {/* Navigation stays here, outside the Switch */}
          <nav>
            <Link to="/#story" className="font-medium text-gray-100 hover:text-red-600">Story</Link>
            <Link to="/about" className="text-base text-gray-300 hover:text-white"> About </Link>
          </nav>
    
          <Switch>
            {/* Main page route (only renders at exact path "/") */}
            <Route path="/" exact>
              <div id="story" className="py-16 xl:py-36 px-4 sm:px-6 lg:px-8 bg-black overflow-hidden">
                {/* Your main page content goes here */}
              </div>
            </Route>
    
            {/* About page route */}
            <Route path="/about">
              <About />
            </Route>
          </Switch>
        </Router>
      );
    }
    
  3. Add the exact prop to the root route: This ensures your main page only renders when the URL is exactly /, not when it’s /about or any other subpath.

Now when you click the About link, React will handle the route client-side (no full reload), and only the About component will render alongside your persistent navigation.


内容的提问来源于stack exchange,提问作者reallymemorable

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 22:27:34