为什么 React 导航不加载新页面?

发布于 2025-01-10 17:15:30 字数 4087 浏览 0 评论 0原文

我正在尝试构建一个反应应用程序,但导航似乎无法正常工作。当我加载页面时,它显示带有“推文”和“创建推文”的导航栏,但单击它们不会加载页面。我正在使用带有

我一整天都在研究这个问题,但似乎无法弄清楚。知道我做错了什么吗?

index.js:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import '../node_modules/bootstrap/dist/css/bootstrap.min.css';

import App from './App';
import { BrowserRouter } from 'react-router-dom';
import reportWebVitals from './reportWebVitals';

ReactDOM.render(
  <React.StrictMode>
    <BrowserRouter>
      <App />
    </BrowserRouter>
  </React.StrictMode>,
  document.getElementById('root')
);

App.js:

import React from 'react';
import { 
  BrowserRouter as Router,
  Routes, 
  Route,
  Link
} from "react-router-dom";
import { TweetList } from './TweetList';
import { CreateTweet } from './CreateTweet';

function App() {
  return (
      <div>
        <nav className="navbar bg-light navbar-expand-lg navbar-light">
          <ul className="navbar-nav mr-auto">
            <li className="navbar-item">
              <Link to="/" className="nav-link">Tweets</Link>
            </li>
            <li className="navbar-item">
              <Link to="/create" className="nav-link">Create Tweet</Link>
            </li>
          </ul>
        </nav>
        <Routes>
          <Route exact path="/" element={TweetList} />
          <Route path="/create" element={CreateTweet} />
        </Routes>

      </div>
  );
}

export default App;

TweetList.js:

import React, { useState, useEffect } from "react";
import { Link } from 'react-router-dom'
   
  return (
    <div className="container">
      <div className="mt-3">
        <h3>Tweet List</h3>
        <table className="table table-striped mt-3">
          <thead>
            <tr>
              <th>Text</th>
              <th>Action</th>
            </tr>
          </thead>
        </table>
      </div>
    </div>
  );
};

CreateTweet:

import React, { useRef } from 'react';
import { useNavigate } from "react-router-dom";

export const CreateTweet = () => {
    return (
        <div className="container">
            <div className="mt-3">
                <h3>Create Tweet Item</h3>
            </div>
        </div>
    );
}

package.json:

{
  "name": "twitter-clone",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.2",
    "@testing-library/react": "^12.1.3",
    "@testing-library/user-event": "^13.5.0",
    "bootstrap": "^5.1.3",
    "react": "^17.0.2",
    "react-bootstrap": "^2.1.2",
    "react-dom": "^17.0.2",
    "react-hook-form": "^7.27.1",
    "react-router-dom": "^6.2.1",
    "react-scripts": "5.0.0",
    "web-vitals": "^2.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

编辑 1:这是我正在处理的文件的存储库: https://github.com/adam-weiler/twitter-clone

编辑2: 通过更改 App.js 中的这两行,它现在似乎可以工作:

不起作用:

<Route path="/" element={TweetList} />
<Route path="/create" element={CreateTweet} />

工作:

<Route path="/" element={<TweetList />} />
<Route path="/create" element={<CreateTweet />} />

I'm trying to build a react app but the navigation doesn't seem to be working properly. When I load the page it's showing the Navbar with "Tweets" and "Create Tweet", but clicking on them isn't loading the page. I was using an older tutorial with <Switch and I'm trying to update it to Routes>.

I've been working on this all day and can't seem to figure it out. Any idea what I've done wrong?

index.js:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import '../node_modules/bootstrap/dist/css/bootstrap.min.css';

import App from './App';
import { BrowserRouter } from 'react-router-dom';
import reportWebVitals from './reportWebVitals';

ReactDOM.render(
  <React.StrictMode>
    <BrowserRouter>
      <App />
    </BrowserRouter>
  </React.StrictMode>,
  document.getElementById('root')
);

App.js:

import React from 'react';
import { 
  BrowserRouter as Router,
  Routes, 
  Route,
  Link
} from "react-router-dom";
import { TweetList } from './TweetList';
import { CreateTweet } from './CreateTweet';

function App() {
  return (
      <div>
        <nav className="navbar bg-light navbar-expand-lg navbar-light">
          <ul className="navbar-nav mr-auto">
            <li className="navbar-item">
              <Link to="/" className="nav-link">Tweets</Link>
            </li>
            <li className="navbar-item">
              <Link to="/create" className="nav-link">Create Tweet</Link>
            </li>
          </ul>
        </nav>
        <Routes>
          <Route exact path="/" element={TweetList} />
          <Route path="/create" element={CreateTweet} />
        </Routes>

      </div>
  );
}

export default App;

TweetList.js:

import React, { useState, useEffect } from "react";
import { Link } from 'react-router-dom'
   
  return (
    <div className="container">
      <div className="mt-3">
        <h3>Tweet List</h3>
        <table className="table table-striped mt-3">
          <thead>
            <tr>
              <th>Text</th>
              <th>Action</th>
            </tr>
          </thead>
        </table>
      </div>
    </div>
  );
};

CreateTweet:

import React, { useRef } from 'react';
import { useNavigate } from "react-router-dom";

export const CreateTweet = () => {
    return (
        <div className="container">
            <div className="mt-3">
                <h3>Create Tweet Item</h3>
            </div>
        </div>
    );
}

package.json:

{
  "name": "twitter-clone",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.2",
    "@testing-library/react": "^12.1.3",
    "@testing-library/user-event": "^13.5.0",
    "bootstrap": "^5.1.3",
    "react": "^17.0.2",
    "react-bootstrap": "^2.1.2",
    "react-dom": "^17.0.2",
    "react-hook-form": "^7.27.1",
    "react-router-dom": "^6.2.1",
    "react-scripts": "5.0.0",
    "web-vitals": "^2.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

Edit 1: This is a repo of the files I'm working on:
https://github.com/adam-weiler/twitter-clone

Edit 2:
By changing these 2 lines in App.js it seems to be working now:

Doesn't work:

<Route path="/" element={TweetList} />
<Route path="/create" element={CreateTweet} />

Working:

<Route path="/" element={<TweetList />} />
<Route path="/create" element={<CreateTweet />} />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

箜明 2025-01-17 17:15:30

首先,您忘记将 Routes 元素包装在您导入的 Router 中,但未添加到返回的 jsx 中。
将 div 包裹在 ... 内应该可以解决路由问题。

其次,您传递给 Route.element 属性的是一个组件而不是元素,我建议使用 Route.component 属性或 Route.render 需要一个返回元素的函数。

Well first of all, you forgot to wrap the Routes element inside the Router, which you imported, but not added to the returned jsx.
Wrapping the div inside <Router>...</Router> should fix the routing issue.

Second of all you pass to the Route.element prop a component not an element, I suggest using either the Route.component prop or Route.render which expects a function that returns an element.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文