我正在开发一个React应用程序,其中正在实现文件系统,例如上传文件和创建文件夹等(小保管箱)。我正在从以下链接中了解有关React Router递归路径的信息:https://reacttraining.com/react-router/web/example/recursive-paths
该组件正在同一页面上呈现。我想执行这种递归模式,而不是在同一页面上重新呈现,我只想呈现最新的路由数据,就像在cpanel或dropbox之类的服务器上使用文件系统时一样。任何帮助,将不胜感激。
App.js
<Switch>
<Route
path={"/files/:title"} component={FolderDetail} />
</Switch>
FolderDetail.js
import React, { useState } from "react";
import { Switch, Route } from "react-router-dom";
import { Row, Col } from "reactstrap";
import UploadOptionsSidebar from "../components/Sidebar/UploadOptionsSidebar";
import BrowseFiles from "../components/BrowseFiles/BrowseFiles";
import CreateFolder from "../components/CreateFolder/CreateFolder";
import { AppContext } from "../components/Context/main";
const FolderDetail = ({ match, child }) => {
const { files } = React.useContext(AppContext);
const [createFolder, setCreateFolder] = useState(false);
const getFiles = () => {
return files.filter((file) => file.parent === match.params.title);
};
return (
<div>
<React.Fragment>
<h3>
Files >{" "}
<span className={"text-muted"}>{match.params.title}</span>{" "}
</h3>
<Row className={"mt-5 mx-0"}>
<Col md={"8"}>
<BrowseFiles files={getFiles()} />
</Col>
<Col md={"3"}>
<UploadOptionsSidebar
openCreateFolder={() => setCreateFolder(true)}
/>
</Col>
</Row>
<CreateFolder
open={createFolder}
parent={match.params.title}
toggle={() => setCreateFolder(false)}
/>
</React.Fragment>
<Switch>
<Route
path={`${match.url}/:title`}
render={() => <FolderDetail match={match} child={true} />}
/>
</Switch>
</div>
);
};
export default FolderDetail;
最佳答案
如何使用诸如<Route path="/files" component={Files} />
之类的非exact路径以及在this.props.location.pathname
组件中使用Files
(请参阅location docs)从该路径中使用正则表达式提取所需的数据,而仅呈现满足您需要的内容(例如 View )相对于路径的最后一部分)?
关于javascript - 如何处理React Router的递归路径?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60848592/