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

求助:使用最新版create-react-app,React Router返回空白页且无报错

Troubleshooting Your Unrendered React Components

Hey there, let's figure out why your Home and About components aren't showing up even though your app runs without obvious errors! Based on what you've shared, here are the most common fixes to check:

1. Fix Incomplete/Unclosed JSX Tags

Your snippet cuts off at <di...—this is a huge red flag! JSX requires all tags to be properly closed, either as self-closing tags like <img /> or paired tags like <div>...</div>. If you left a tag open (like starting a <div> but never adding </div>), React will fail to render the component silently (or throw a hidden error in the console).

Here's a corrected example of your About component:

import React from 'react';

const About = () => (
  <div>
    <h2>About Us</h2>
    <p>Your about page content goes here!</p>
  </div>
);

export default About;

2. Verify Component Exports & Imports

Double-check two key steps here:

  • Export the component: Make sure each component ends with export default About; (or export default Home; for the other file). Without this, your app can't import and use the component.
  • Import correctly in your parent component: In App.js (or wherever you're rendering these components), confirm the import path is right. For example:
    import Home from './Home';
    import About from './About';
    
    If your components are in a pages folder, the path would be ./pages/Home instead.

3. Ensure Components Are Actually Rendered in Your App

It sounds obvious, but make sure you're actually using the components in your root App component (or routing setup if you're using React Router):

  • Basic setup (no routing):
    function App() {
      return (
        <div className="App">
          <Home />
          <About />
        </div>
      );
    }
    
  • React Router v6 setup:
    import { Routes, Route } from 'react-router-dom';
    
    function App() {
      return (
        <Routes>
          <Route path="/" element={<Home />} />
          <Route path="/about" element={<About />} />
        </Routes>
      );
    }
    

4. Check Browser Console for Hidden Errors

Even if your app seems to run, open your browser's developer tools (F12 → Console tab) and look for red error messages. Common issues here include:

  • Missing root elements (React requires JSX to have a single parent; use <></> Fragment if you don't want an extra div)
  • Typos in component names (e.g., importing about instead of About)
  • Undefined variables used in the component

5. Confirm You're Using Valid JSX Syntax

If you're switching between functional and class components, make sure you're following the right syntax:

  • Class component example:
    import React, { Component } from 'react';
    
    class About extends Component {
      render() {
        return (
          <div>
            <h2>About Page (Class Component)</h2>
          </div>
        );
      }
    }
    
    export default About;
    

Start with checking the unclosed tags and console errors first—those are the most likely culprits here!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 09:00:23