【问题标题】:React components not rendering when routing to new page with react-router-dom v6使用 react-router-dom v6 路由到新页面时,React 组件未呈现
【发布时间】:2021-10-21 12:13:41
【问题描述】:

我正在尝试在我的 React 应用程序中设置路由。我没有收到任何错误,应用程序也没有中断,但是当我导航到新页面时,什么也没有显示。我正在使用 react-router-dom v6 并且 'Switch' 已替换为 'Routes' 并且组件已替换为元素:https://reacttraining.com/blog/react-router-v6-pre/。看来您不需要前导“/”。

在页面底部编辑。

http://localhost:3000/Playerhttp://localhost:3000/*(或除 * 以外的任何内容)都呈现空白屏幕。

这是运行良好的着陆页:

主页.tsx

/** @jsxImportSource @emotion/react */
import React from 'react';
import { css } from '@emotion/react';
import logo from '../assets/cyberpunk.png';

export const HomePage = () => (
  <div
    css={css`
      margin: 10px auto 20px auto;
      padding: 30px 20px;
      max-width: 1000px;
      display: flex;
      align-items: center;
      justify-content: space-between;
    `}
  >
    <div>
      <img
        src={logo}
        alt="Logo"
        css={css`
          width: 700px;
          margin-left: 130px;
        `}
      />
      <h2>Players</h2>
      <button>Select a toon</button>
    </div>
  </div>
);

这是播放器页面:

PlayerPage.tsx

import React from 'react';

export const PlayerPage = () => <h2>"One toon"</h2>;

找不到页面的页面:

NotFoundPage.tsx

import React from 'react';

export const NotFoundPage = () => <h2>"Page Not Found"</h2>;

最后,

App.tsx

/** @jsxImportSource @emotion/react */
import React from 'react';
import { css } from '@emotion/react';
import { Header } from './header';
import { HomePage } from './Views/HomePage';
import { fontFamily, fontSize, gray2 } from './Styles';
import { BrowserRouter, Routes, Route } from 'react-router-dom';
import { PlayerPage } from './Views/PlayerPage';
import { PlayersPage } from './Views/PlayersPage';
import { PlayerCreatePage } from './Views/PlayerCreatePage';
import { NotFoundPage } from './Views/NotFoundPage';

function App() {
  return (
    <BrowserRouter>
      <div
        css={css`
          font-family: ${fontFamily};
          font-size: ${fontSize};
          color: ${gray2};
        `}
      >
        <Header />
        <Routes>
          <Route path="" element={<HomePage />} />
          <Route path="Players" element={<PlayersPage />} />
          <Route path="Player" element={<PlayerPage />} />
          <Route path="Create" element={<PlayerCreatePage />} />
          <Route path="*" element={<NotFoundPage />} />
        </Routes>
      </div>
    </BrowserRouter>
  );
}

export default App;

为什么 NotFoundPage 和 PlayerPage 中的 h2 元素不会显示?这就像我每次都在导航到错误的路线。

我修好了。我重新制作了其中一个页面,如下所示:

function PlayerCreatePage() {
  return (
    <div style={{ padding: 20 }}>
      <h2>About View</h2>
      <p>Lorem ipsum dolor sit amet, consectetur adip.</p>
    </div>
  );
}

export default PlayerCreatePage;

这个确实出现了。这是为什么?其他观点有什么问题?

【问题讨论】:

  • 我认为您在所有路径值之前都缺少反斜杠。我也不确定您使用的是哪个版本的 react-router-dom。我期待一个 Switch 组件而不是 Routes,以及“component”Route prop 而不是“element”。
  • @Mary 看起来在 react-router-dom v6 'Switch' 已被 'Routes' 替换。
  • @GrillOwner69420 你能解决这个问题吗?有这方面的更新吗?

标签: javascript reactjs react-router react-router-dom


【解决方案1】:

我试图重现您的错误,但无法重现。起初,我认为您没有在函数中编写 return() 可能是个问题,但后来我意识到您导出时没有大括号,所以 return 不是必需的。无论如何,代码运行良好,您应该尝试在新环境中重新创建。和平!

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
【解决方案2】:

在每个路径属性前加一个“/”,我认为你应该阅读react-router-dom docs

【讨论】:

  • 这并没有解决问题。你不需要使用 / 因为 React Router 默认执行相对映射匹配。
【解决方案3】:

问题出在App.tsx文件中,将element属性改为component,将通用路由移至底部并添加准确 在特定路线的前面。 请参阅以下更改:- `

/** @jsxImportSource @emotion/react */
import React from 'react';
import { css } from '@emotion/react';
import { Header } from './header';
import { HomePage } from './Views/HomePage';
import { fontFamily, fontSize, gray2 } from './Styles';
import { BrowserRouter, Routes, Route } from 'react-router-dom';
import { PlayerPage } from './Views/PlayerPage';
import { PlayersPage } from './Views/PlayersPage';
import { PlayerCreatePage } from './Views/PlayerCreatePage';
import { NotFoundPage } from './Views/NotFoundPage';

function App() {
  return (
   <BrowserRouter>
    <div
    css={css`
      font-family: ${fontFamily};
      font-size: ${fontSize};
      color: ${gray2};
    `}
  >
    <Header />
    <Routes>
      <Route exact path="/Players" component={<PlayersPage />} />
      <Route exact path="/Player" component={<PlayerPage />} />
      <Route exact path="/Create" component={<PlayerCreatePage />} />
      <Route path="/" component={<HomePage />} /> 
     <Route path="*" component={<NotFoundPage />} />
    </Routes>
  </div>
</BrowserRouter>

); }

导出默认应用; ` 它会起作用的。

【讨论】:

  • 你的代码给了我这个错误:````类型'{精确:真;路径:字符串;组件:元素; }' 不可分配给类型 'IntrinsicAttributes & RouteProps'。类型'IntrinsicAttributes & RouteProps'```` 上不存在属性'exact'
最近更新 更多