gpt4 book ai didi

javascript - BrowserRouter 中的链接标签仅更改 URL,但不呈现组件

转载 作者:行者123 更新时间:2023-12-05 00:27:10 26 4
gpt4 key购买 nike

我正在构建一个 Netflix 克隆应用程序,并且正在使用 react-router-dom v5在不同页面之间切换。但是,当我单击 Link标记 Navbar.jsx ,URL 会发生变化,但相应的组件不会呈现。我咨询过很多StackOverflow posts然而,在这个话题上,我无法让它发挥作用。下面是代码。请帮助我,因为我坚持了 3 天😥。
导航到 /series 时应显示的内容来自 / :
enter image description here
它实际显示的内容:
enter image description here

  • index.js
  • import React from "react";
    import { createRoot } from "react-dom/client";
    import App from "./App";

    const container = document.getElementById("root");
    const root = createRoot(container);

    root.render(
    <React.StrictMode>
    <App />
    </React.StrictMode>
    );
  • 应用.jsx
  • import React from "react";

    import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
    import "./app.scss";
    import Home from "./pages/home/Home";
    import Watch from "./pages/watch/Watch";

    const App = () => {
    return (
    <Router>
    <Switch>
    <Route exact path="/">
    <Home />
    </Route>
    <Route path="/movies">
    <Home type="movies" />
    </Route>
    <Route path="/series">
    <Home type="series" />
    </Route>
    <Route path="/watch">
    <Watch />
    </Route>
    </Switch>
    </Router>
    );
    };

    export default App;
  • 主页.jsx

  • import React from "react";
    import Featured from "../../components/featured/Featured";
    import Navbar from "../../components/navbar/Navbar";

    import "./home.scss";

    const Home = ({ type }) => {
    return (
    <div className="home">
    <Navbar />
    <Featured type={type} />
    </div>
    );
    };

    export default Home;
  • 导航栏.jsx

  • import React, { useState } from "react";

    import "./navbar.scss";
    import { Link } from "react-router-dom";

    const Navbar = () => {
    const [isScrolled, setIsScrolled] = useState(false);

    window.onscroll = () => {
    setIsScrolled(window.scrollY === 0 ? false : true);
    return () => window.onscroll == null;
    };

    return (
    <div className={`navbar ${isScrolled ? "scrolled" : ""}`}>
    <div className="container">
    <img src="./netflix_logo.jpg"
    alt="netflix logo"
    />
    <Link to="/" className="link">
    <span>Home</span>
    </Link>
    <Link to="/series" className="link">
    <span>Series</span>
    </Link>
    <Link to="/movies" className="link">
    <span>Movies</span>
    </Link>
    <Link to="" className="link">
    <span>New and Popular</span>
    </Link>
    <Link to="" className="link">
    <span>My List</span>
    </Link>
    </div>
    </div>
    );
    };

    export default Navbar;
  • 精选.jsx
  • import "./featured.scss";
    import { IoMdPlay } from "react-icons/io";
    import { FiInfo } from "react-icons/fi";

    const Featured = ({ type }) => {
    return (
    <div className="featured">
    {type && (
    <div className="category">
    <span style={{ color: "white" }}>
    {type === "movies" ? "Movies" : "TV Series"}
    </span>
    <select name="genre" id="genre">
    <option>Genre</option>
    <option value="adventure">Adventure</option>
    <option value="comedy">Comedy</option>
    <option value="crime">Crime</option>
    <option value="fantasy">Fantasy</option>
    <option value="historical">Historical</option>
    <option value="horror">Horror</option>
    <option value="romance">Romance</option>
    <option value="sci-fi">Sci-fi</option>
    <option value="thriller">Thriller</option>
    <option value="western">Western</option>
    <option value="animation">Animation</option>
    <option value="drama">Drama</option>
    <option value="documentary">Documentary</option>
    </select>
    </div>
    )}
    <img
    src="https://m.media-amazon.com/images/M/MV5BNzM4OTkzMjcxOF5BMl5BanBnXkFtZTgwMTkxMjI1MTI@._V1_.jpg"
    alt="featured"
    />
    <div className="info">
    <img
    src="https://occ-0-1432-1433.1.nflxso.net/dnm/api/v6/LmEnxtiAuzezXBjYXPuDgfZ4zZQ/AAAABUZdeG1DrMstq-YKHZ-dA-cx2uQN_YbCYx7RABDk0y7F8ZK6nzgCz4bp5qJVgMizPbVpIvXrd4xMBQAuNe0xmuW2WjoeGMDn1cFO.webp?r=df1"
    alt=""
    />
    <span className="desc">
    When a beautiful stranger leads computer hacker Neo to a forbidding
    underworld, he discovers the shocking truth - the life he knows is the
    elaborate deception of an evil cyber-intelligence.
    </span>
    <div className="buttons">
    <button className="play">
    <IoMdPlay className="button-logo" />
    <span>Play</span>
    </button>
    <button className="more">
    <FiInfo className="button-logo" />
    <span>More Info</span>
    </button>
    </div>
    </div>
    </div>
    );
    };

    export default Featured;

    最佳答案

    react-router-dom@5 的 5.3.3 之前版本之间存在兼容性问题和 react@18 .

  • Github Issue #7870
  • PR #8831 merged to address issue - 修复于 2022 年 5 月 18 日合并,react-router-dom v5.3.3。

  • 解决方案
  • 错误修复已合并到 v5.3.3。更新至 react-router-dom@5.3.3或更高。
    从项目的根目录运行:
  • npm uninstall -S react-router-dom
  • npm install -S react-router-dom@5.3.3 (或 @latest )

  • Edit link-tag-inside-browserrouter-changes-only-the-url-but-doesnt-render-the-compo (forked)
  • 恢复到 React 17(或 React 17 语法)并修复 index.js文件。
    import { StrictMode } from "react";
    import ReactDOM from "react-dom";

    import App from "./App";

    ReactDOM.render(
    <StrictMode>
    <App />
    </StrictMode>,
    document.getElementById("root")
    );
    Edit link-tag-inside-browserrouter-changes-only-the-url-but-doesnt-render-the-compo
  • 制作 React.StrictMode组件路由器组件的子/后代。 Comment .
    代替:
    <React.StrictMode>
    ...
    <BrowserRouter>
    ...
    </BrowserRouter>
    </React.StrictMode>
    和:
    <BrowserRouter>
    <React.StrictMode>
    ...
    </React.StrictMode>
    </BrowserRouter>
    Edit link-tag-inside-browserrouter-changes-only-the-url-but-doesnt-render-the-compo (forked)
  • 升级到 react-router-dom@6并修复路线。
    const App = () => {
    return (
    <Router>
    <Routes>
    <Route path="/" element={<Home />} />
    <Route path="/movies" element={<Home type="movies" />} />
    <Route path="/series" element={<Home type="series" />} />
    <Route path="/watch" element={<Watch />} />
    </Routes>
    </Router>
    );
    }
    Edit link-tag-inside-browserrouter-changes-only-the-url-but-doesnt-render-the-compo (forked)
  • 关于javascript - BrowserRouter 中的链接标签仅更改 URL,但不呈现组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71832720/

    26 4 0