Skip to content

Commit

Permalink
feat: dynamic routes with examples (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
Param-Harrison committed Apr 15, 2019
1 parent 3db2531 commit f6935df
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/App.js
Expand Up @@ -2,6 +2,18 @@ import React from 'react';
import { Link, BrowserRouter as Router, Route } from 'react-router-dom';
import './App.css';

const users = [
{
name: `Param`,
},
{
name: `Vennila`,
},
{
name: `Afrin`,
},
];

const IndexPage = () => {
return (
<h3>Home Page</h3>
Expand All @@ -14,13 +26,47 @@ const AboutPage = () => {
);
};

const UsersPage = () => {
return (
<>
{
users.map((user, index) => (
<h5 key={index}>
<Link to={`/user/${index + 1}`}>{user.name}'s Page</Link>
</h5>
))
}
</>
);
};

const UserPage = ({ match, location }) => {
const { params: { userId } } = match;

return (
<>
<p>
<strong>User ID: </strong>
{userId}
</p>
<p>
<strong>User Name: </strong>
{users[userId - 1].name}
</p>
</>
);
};

const App = () => {
return (
<section className="App">
<Router>
<Link to="/">Home</Link>
<Link to="/about">About</Link>
<Link to="/users">Users</Link>
<Route exact path="/" component={IndexPage} />
<Route exact path="/users" component={UsersPage} />
<Route exact path="/user/:userId" component={UserPage} />
<Route exact path="/about" component={AboutPage} />
</Router>
<a href="/about">about with browser reload</a>
Expand Down

0 comments on commit f6935df

Please sign in to comment.