Skip to content

Commit

Permalink
Routes as object
Browse files Browse the repository at this point in the history
  • Loading branch information
collegewap committed Sep 11, 2020
1 parent 2e86f35 commit fbe1b31
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from "react";
import { Routes, Route, NavLink as Link } from "react-router-dom";
import Dashboard from "./Dashboard";
import RouteAsObj from "./RouteAsObj";

function App() {
return (
Expand All @@ -22,13 +23,20 @@ function App() {
About
</Link>
</li>
<li>
<Link to="/object_route" activeClassName="active">
Route as Object
</Link>
</li>
</ul>
</nav>
<div className="main">
<Routes>
<Route path="/" element={<Home />}></Route>
<Route path="about" element={<About />}></Route>
<Route path="dashboard/*" element={<Dashboard />}></Route>
<Route path="object_route/*" element={<RouteAsObj />}></Route>

<Route path="*" element={<NotFound />}></Route>
</Routes>
</div>
Expand Down
60 changes: 60 additions & 0 deletions src/RouteAsObj.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import React from "react";
import { useRoutes, Outlet } from "react-router";
import { Link } from "react-router-dom";

const RouteAsObj = () => {
let element = useRoutes([
{ path: "/", element: <Route1 /> },
{ path: "route2", element: <Route2 /> },
{
path: "route3",
element: <Route3 />,
children: [
{ path: "child1", element: <Child1 /> },
{ path: "child2", element: <Child2 /> },
],
},
{ path: "*", element: <NotFound /> },
]);

return (
<div>
<ul>
<li>
<Link to="">Route1</Link>
</li>
<li>
<Link to="route2">Route2</Link>
</li>
<li>
<Link to="route3">Route3</Link>
</li>
</ul>
{element}
</div>
);
};

const Route1 = () => <h1>Route1</h1>;
const Route2 = () => <h1>Route2</h1>;
const Route3 = () => {
return (
<div>
<h1>Route3</h1>
<ul>
<li>
<Link to="child1">Child1</Link>
</li>
<li>
<Link to="child2">Child2</Link>
</li>
</ul>
<Outlet />
</div>
);
};
const Child1 = () => <h2>Child1</h2>;
const Child2 = () => <h2>Child2</h2>;
const NotFound = () => <h1>NotFound</h1>;

export default RouteAsObj;

0 comments on commit fbe1b31

Please sign in to comment.