BrowserRouter显示空白页面。

huangapple go评论73阅读模式
英文:

BrowserRouter Showing blank Page

问题

以下是您提供的代码的翻译部分:

App.js

import { BrowserRouter, Route, Routes } from "react-router-dom";
import Header2 from "./components/Header";
import HomePage from "./pages/HomePage";
import Blog from "./pages/Blog";
import SinglePost from "./pages/SinglePost";
import Error from "./pages/Error";

function App() {
  return (
    <BrowserRouter>
      <Header2 />
      <Routes>
        <Route path="/" element={<HomePage />} />
        <Route path="/blog" element={<Blog />} />
        <Route path="/blog/:slug" element={<SinglePost />} />
        <Route path="/" element={<Error />} />
      </Routes>
    </BrowserRouter>
  );
}

export default App;

Header.js

import React from 'react';
import { Link } from 'react-router-dom';

export default function Header2() {
  return (
    <>
      <h1>hi</h1>
      <header>
        <div className='logo'>
          <Link to="/">
            AndiiCodes Blog
          </Link>
        </div>
        <nav>
          <ul>
            <li>
              <button>
                <Link to="/blog" relative="path">Blog</Link>
              </button>
            </li>
            <li>
              <button>
                <Link to="/" relative="path">Home</Link>
              </button>
            </li>
          </ul>
        </nav>
      </header>
    </>
  );
}

请注意,我已经将 Erorr 更正为 Error

此外,您提供的其他文本没有要翻译的内容,因此不会返回其他内容。如果您需要进一步的帮助或有其他问题,请随时提出。

英文:

I'm following a YouTube video tutorial that creates a blog with React and Sanity CMS. Everything went well until I added some code. It keeps showing a blank page. The Video I'm following is using { Switch } but I did some research and found that { Switch } no longer works in V6.

Here's my code:

App.js

import { BrowserRouter, Route, Routes } from &quot;react-router-dom&quot;;
import Header2 from &quot;./components/Header&quot;;
import HomePage from &quot;./pages/HomePage&quot;;
import Blog from &quot;./pages/Blog&quot;;
import SinglePost from &quot;./pages/SinglePost&quot;;
import Erorr from &quot;./pages/Erorr&quot;;

function App() {
  return (
    &lt;BrowserRouter&gt;
      &lt;Header2 /&gt;
      &lt;Routes&gt;
        &lt;Route path=&quot;/&quot; element={&lt;HomePage /&gt;} /&gt;
        &lt;Route path=&quot;/blog&quot; element={&lt;Blog /&gt;} /&gt;
        &lt;Route path=&quot;/blog/:slug&quot; element={&lt;SinglePost /&gt;} /&gt;
        &lt;Route path=&quot;/&quot; element={&lt;Erorr /&gt;} /&gt;
      &lt;/Routes&gt;
    &lt;/BrowserRouter&gt;
  );
}

export default App;

Header.js

import React from &#39;react&#39;
import { Link } from &#39;react-router-dom&#39;
export default function Header2() {
  return (
    &lt;&gt;
      &lt;h1&gt;hi&lt;/h1&gt;
      &lt;header&gt;
        &lt;div className=&#39;logo&#39;&gt;
          &lt;Link to=&quot;/&quot;&gt;
            AndiiCodes Blog
          &lt;/Link&gt;
        &lt;/div&gt;
        &lt;nav&gt;
          &lt;ul&gt;
            &lt;li&gt;
              &lt;button&gt;
                &lt;Link to=&quot;/blog&quot; relative=&quot;path&quot;&gt;Blog&lt;/Link&gt;
              &lt;/button&gt;
            &lt;/li&gt;
            &lt;li&gt;
              &lt;button&gt;
                &lt;Link to=&quot;/&quot; relative=&quot;path&quot;&gt;Home&lt;/Link&gt;
              &lt;/button&gt;
            &lt;/li&gt;
          &lt;/ul&gt;
        &lt;/nav&gt;
      &lt;/header&gt;
    &lt;/&gt;
  )
}

Blank Screenshot

screenshot

files structure

files structure

Console Erorrs:

Warning: React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object. You likely forgot to export your component from the file it&#39;s defined in, or you might have mixed up default and named imports.

Check your code at App.js:13.
    at App


Uncaught TypeError: Cannot read properties of null (reading &#39;useRef&#39;)
    at Object.useRef (react.development.js:1630:1)
    at BrowserRouter (index.tsx:292:1)
    at renderWithHooks (react-dom.development.js:16305:1)
    at mountIndeterminateComponent (react-dom.development.js:20074:1)
    at beginWork (react-dom.development.js:21587:1)
    at HTMLUnknownElement.callCallback (react-dom.development.js:4164:1)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:4213:1)
    at invokeGuardedCallback (react-dom.development.js:4277:1)
    at beginWork$1 (react-dom.development.js:27451:1)
    at performUnitOfWork (react-dom.development.js:26557:1)
react-dom.development.js:18687 The above error occurred in the &lt;BrowserRouter&gt; component:

    at BrowserRouter (http://localhost:3000/static/js/bundle.js:43763:5)
    at App

Consider adding an error boundary to your tree to customize error handling behavior.
Visit https://reactjs.org/link/error-boundaries to learn more about error boundaries.

package.json

{
  &quot;name&quot;: &quot;blog&quot;,
  &quot;version&quot;: &quot;0.1.0&quot;,
  &quot;private&quot;: true,
  &quot;dependencies&quot;: {
    &quot;@testing-library/jest-dom&quot;: &quot;^5.16.5&quot;,
    &quot;@testing-library/react&quot;: &quot;^13.4.0&quot;,
    &quot;@testing-library/user-event&quot;: &quot;^13.5.0&quot;,
    &quot;react&quot;: &quot;^18.2.0&quot;,
    &quot;react-dom&quot;: &quot;^18.2.0&quot;,
    &quot;react-scripts&quot;: &quot;5.0.1&quot;,
    &quot;web-vitals&quot;: &quot;^2.1.4&quot;
  },
  &quot;scripts&quot;: {
    &quot;start&quot;: &quot;react-scripts start&quot;,
    &quot;build&quot;: &quot;react-scripts build&quot;,
    &quot;test&quot;: &quot;react-scripts test&quot;,
    &quot;eject&quot;: &quot;react-scripts eject&quot;
  },
  &quot;eslintConfig&quot;: {
    &quot;extends&quot;: [
      &quot;react-app&quot;,
      &quot;react-app/jest&quot;
    ]
  },
  &quot;browserslist&quot;: {
    &quot;production&quot;: [
      &quot;&gt;0.2%&quot;,
      &quot;not dead&quot;,
      &quot;not op_mini all&quot;
    ],
    &quot;development&quot;: [
      &quot;last 1 chrome version&quot;,
      &quot;last 1 firefox version&quot;,
      &quot;last 1 safari version&quot;
    ]
  }
}

I rechecked all the files names, I tried to remove &lt;BrowserRouter&gt; and all the Routes, the header showed up with no problems. When I'm adding the &lt;BrowserRouter&gt; it's displaying blank again.

答案1

得分: 1

You are missing react-router-dom as a dependency.

"dependencies": {
  "@testing-library/jest-dom": "^5.16.5",
  "@testing-library/react": "^13.4.0",
  "@testing-library/user-event": "^13.5.0",
  "react": "^18.2.0",
  "react-dom": "^18.2.0",
  "react-scripts": "5.0.1",
  "web-vitals": "^2.1.4"
},

To add it to your project and update the package.json file run the following in the root directory of the project: npm i -S react-router-dom@6

Then restart the project: npm start

英文:

You are missing react-router-dom as a dependency.

&quot;dependencies&quot;: {
  &quot;@testing-library/jest-dom&quot;: &quot;^5.16.5&quot;,
  &quot;@testing-library/react&quot;: &quot;^13.4.0&quot;,
  &quot;@testing-library/user-event&quot;: &quot;^13.5.0&quot;,
  &quot;react&quot;: &quot;^18.2.0&quot;,
  &quot;react-dom&quot;: &quot;^18.2.0&quot;,
  &quot;react-scripts&quot;: &quot;5.0.1&quot;,
  &quot;web-vitals&quot;: &quot;^2.1.4&quot;
},

To add it to your project and update the package.json file run the following in the root directory of the project: npm i -S react-router-dom@6

Then restart the project: npm start

huangapple
  • 本文由 发表于 2023年2月10日 04:13:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75403957.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定