Skip to content

Instantly share code, notes, and snippets.

@jacobwallenberg
Created December 7, 2017 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacobwallenberg/9673772e557981df482d0456433f2714 to your computer and use it in GitHub Desktop.
Save jacobwallenberg/9673772e557981df482d0456433f2714 to your computer and use it in GitHub Desktop.
import React from 'react'
import {
BrowserRouter as Router,
Route,
Link
} from 'react-router-dom'
const PEEPS = [
{ id: 0, name: 'Michelle', friends: [ 1, 2, 3 ] },
{ id: 1, name: 'Sean', friends: [ 0, 3 ] },
{ id: 2, name: 'Kim', friends: [ 0, 1, 3 ], },
{ id: 3, name: 'David', friends: [ 1, 2 ] }
]
const find = (id) => PEEPS.find(p => p.id == id)
const RecursiveExample = () => (
<Router>
<Person match={{ params: { id: 0 }, url: '' }}/>
</Router>
)
const Person = ({ match }) => {
const person = find(match.params.id)
return (
<div>
<h3>{person.name}’s Friends</h3>
<ul>
{person.friends.map(id => (
<li key={id}>
<Link to={`${match.url}/${id}`}>
{find(id).name}
</Link>
</li>
))}
</ul>
<Route path={`${match.url}/:id`} component={Person}/>
</div>
)
}
export default RecursiveExample
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment