Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
frontend/my-app/src/RouterComp.jsx
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
34 lines (32 sloc)
1.1 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
import { Route, Redirect, Switch } from 'react-router-dom' | |
function RouterComp(props) { | |
let { | |
routes | |
} = props; | |
//Here is to find out all items that are redirects | |
let redir = routes && routes.length > 0 && routes.filter((v, i) => { | |
return v.redirect | |
}) | |
//Render the redirected component | |
let renderRedir = redir.length > 0 && redir.map((v, i) => { | |
return <Redirect from={v.path} to={v.redirect} key={i} /> | |
}) | |
return ( | |
<Switch> | |
{ | |
//Here, all Routes are rendered according to its path and corresponding components | |
routes && routes.length > 0 && routes.map((v, i) => { | |
if (v.component) { | |
return <Route key={i} path={v.path} component={(props) => { | |
//Here we pass the children, which is the next level of routing, to facilitate the use of the next level of routing | |
return <v.component routes={v.children} {...props} /> | |
}} /> | |
} | |
//Here we will add the redirected component to it | |
}).concat(renderRedir) | |
} | |
</Switch> | |
) | |
} | |
export default RouterComp |