Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle React Router Recursive Paths?

I'm working on a react app where I'm implementing file system like uploading files and creating folders etc (little dropbox). I was reading about react router recursive paths from this link: https://reacttraining.com/react-router/web/example/recursive-paths There component is being rendered on same page. I want to do that recursive pattern but rather than rerendering on same page, I want to only render latest route data just like when you work with file systems on some server like cpanel or dropbox. Any help would be appreciated.

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;
like image 610
Zayn Avatar asked Mar 25 '20 12:03

Zayn


People also ask

What is the use case of recursive path in react?

Recursive Paths Breadcrumbs offer users a way to trace the path back to their original landing point even after going through multiple routes, and that can be implemented using React Router's functionality, specifically the match object, it provides the ability to write recursive routes for nested child components.

Does react Router allows for nested routes?

With React Router, you have two options for creating nested routes. The first is using the /* with nested <Routes> pattern and the second is using the <Outlet /> pattern.

What are two ways of handling redirect with react Router?

Two ways to handle redirecting on a user event such as create, update and delete with React Router.

Why we use BrowserRouter in react?

React Router is a standard library for routing in React. It enables navigation between views from different components in a React application, allows the browser URL to be changed, and keeps the UI in sync with the URL.


1 Answers

How about using a non exact path such as <Route path="/files" component={Files} /> and using this.props.location.pathname (see location docs) in the Files component to extract the data you need from the path using a regular expression and only render what is needed to serve what your need (for example a view relative to the last part of the path)?

like image 183
sunknudsen Avatar answered Oct 24 '22 14:10

sunknudsen