Created
November 27, 2020 02:41
-
-
Save raphaelsoul/b6b25a0da9aad344e85210ed3bfd6682 to your computer and use it in GitHub Desktop.
nested react router
This file contains hidden or 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 {BrowserRouter, StaticRouter, Switch, Route, Link, Redirect} from "react-router-dom"; | |
| import Hello from "./Hello" | |
| import World from "./World" | |
| const isSSR = typeof window === "undefined" | |
| const Router = isSSR ? StaticRouter : BrowserRouter | |
| export default ({url}) => { | |
| return ( | |
| <Router location={url} > | |
| <ul> | |
| <li> | |
| <Link to="/">Home</Link> | |
| </li> | |
| <li> | |
| <Link to="/user">hello</Link> | |
| </li> | |
| </ul> | |
| <Switch> | |
| <Route path="/" exact>hello, index</Route> | |
| <Route | |
| path="/user" | |
| render={({ match: { url } }) => ( | |
| <> | |
| <Redirect from={`${url}/`} to={`${url}/hello`} /> | |
| <Redirect from={`${url}`} to={`${url}/hello`} /> | |
| <Route path={`${url}/hello`} component={Hello} /> | |
| <Route path={`${url}/world`} component={World} /> | |
| </> | |
| )} | |
| /> | |
| <Route path='*' component={() => <>404</>} /> | |
| </Switch> | |
| </Router> | |
| ) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment