gpt4 book ai didi

javascript - React-router-dom 嵌套在子组件中不起作用

转载 作者:行者123 更新时间:2023-12-04 07:58:40 27 4
gpt4 key购买 nike

为了可视化和清晰的图片,我将首先展示应用程序的屏幕截图。
enter image description here
我正在使用 github api 来获取 Repos 和用户。简单的。现在这就是 App.tsx 的样子;

// App.tsx
import React from "react";
import MainSearchPage from "./Components/MainSearchPage";
import Header from "./Components/Header";
import "./Sass/main.scss";

const App: React.FC = () => {
return (
<div className="App">
<Header />
<MainSearchPage />
</div>
);
};

export default App;

这是 MainSearchPage 组件;
// MainSearchPage.tsx
import React, { useContext } from "react";
import { GithubContext } from "../Context/GithubContext";
import searchPc from "../Icons/search-pc.svg";
import SearchResults from "./SearchResults";

const MainSearchPage: React.FC = () => {
const { isSearched } = useContext(GithubContext);
return (
<div>
{isSearched ? (
<SearchResults />
) : (
<div className="blank-search-page">
<div className="blank-search-page__items">
<img src={searchPc} alt="search-pc" />
<p>Search results will appear here</p>
</div>
</div>
)}
</div>
);
};

export default MainSearchPage;
当输入更改时,isSearch 变为 true 并且 SearchResults 正在呈现。这是 SearchResults,问题开始的地方。
//SearchResults.tsx
import React from "react";
import { Route, BrowserRouter as Router, Switch } from "react-router-dom";
import MainSearchResults from "./MainSearchResults";
import SideBarSearchResults from "./SideBarSearchResults";
import UsersPage from "./UsersPage";

const SearchResults = () => {
return (
<div className="search-results">
<Router>
<SideBarSearchResults />
<Switch>
<Route path="/" component={MainSearchResults} />
<Route path="/search-users" component={UsersPage} />
</Switch>
</Router>
</div>
);
};

export default SearchResults;

所以 MainSearchResults 基本上是右侧的 repo 结果。我想侧边栏很明显。而 UsersPage 是当我单击左侧的用户时应该呈现的组件。但是当我点击时没有任何 react 。网址更改,仅此而已。控制台或任何东西中都没有错误。在我设置“isSearched”状态之前它正在工作。但我认为这不重要。
侧边栏组件;
//SideBarSearchResults.tsx
import React, { useContext } from "react";
import repositoriesSVG from "../Icons/repositories.svg";
import usersSVG from "../Icons/users.svg";
import bookmarkblackSVG from "../Icons/bookmarkblack.svg";
import { Link } from "react-router-dom";
import { GithubContext } from "../Context/GithubContext";
const SideBarSearchResults: React.FC = () => {
const { repoCount, userCount } = useContext(GithubContext);
return (
<div className="side-bar-search-results">
<Link to="/">
<div className="side-bar-search-results__repositories">
<img src={repositoriesSVG} alt="repo" />
<p className="result-title">Repositories</p>
<p className="quantity">{repoCount ?? 0}</p>
</div>
</Link>
<Link to="/search-users">
<div className="side-bar-search-results__users">
<img src={usersSVG} alt="users" />
<p className="result-title">Users</p>
<p className="quantity">{userCount ?? 0}</p>
</div>
</Link>
<div className="side-bar-search-results__bookmarked ">
<img src={bookmarkblackSVG} alt="bookmarked" />
<Link to="/repo-details">
<p className="result-title">Bookmarked</p>
</Link>
<p className="quantity">15</p>
</div>
</div>
);
};

export default SideBarSearchResults;

最佳答案

问题Switch组件匹配并呈现 第一 匹配的路径,“/”是每个路径的前缀。换句话说,它总是首先匹配并返回的路径。
解决方案
重新订购您的 Route s 首先指定更具体的路径,然后按照路径特异性的降序排列。

<Router>
<SideBarSearchResults />
<Switch>
<Route path="/search-users" component={UsersPage} />
<Route path="/" component={MainSearchResults} />
</Switch>
</Router>

关于javascript - React-router-dom 嵌套在子组件中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66578285/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com