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

React Router问题:NoteList组件无法渲染,求解决方案

React Router 中 NoteList 组件无法渲染的解决方法

你遇到的问题来自两个关键错误,以下是具体问题和修复方案:

  • Routes 组件使用方式错误
    React Router v6 中,Routes 是路由容器,必须在其内部嵌套 <Route> 组件来定义路由规则,不能直接将 path 和 element 属性写在 Routes 上。

  • 路由路径设置错误
    路由的 path 对应浏览器地址栏的访问路径,而非本地文件系统路径。你设置的 /src/components/NoteList/NoteList 是文件路径,不符合路由规则,需要改为实际访问的URL路径(比如根路径 /)。

修复后的完整代码

import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
import logoNotes from './img/logoNotes.png';
import NoteList from './components/NoteList/NoteList';
import './App.css';

function App() {
  return (
    <Router>
      <div className='aplication-notes'>
        <div className='notes-logo-container'>
          <img src={logoNotes} className='notes-logo' alt='logo' />
        </div>
        <div className='note-list'>
          <h1> my notes</h1>
          <br></br>
          <Routes>
            {/* 使用Route组件定义路由,path设置为根路径 */}
            <Route path="/" element={<NoteList />} />
          </Routes>
        </div>
      </div>
    </Router>
  );
}

export default App;

额外说明

  • 确保访问项目的根URL(如 http://localhost:3000/),这样路由才能匹配并渲染 NoteList 组件。
  • 如果需要通过其他路径访问,比如 /notes,只需将 path 修改为 /notes,然后访问对应的URL即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 14:35:22