Skip to content

Instantly share code, notes, and snippets.

@shanebarringer
Created September 6, 2017 16:33
Show Gist options
  • Save shanebarringer/79b38975284e4dde3101d75572f8c57c to your computer and use it in GitHub Desktop.
Save shanebarringer/79b38975284e4dde3101d75572f8c57c to your computer and use it in GitHub Desktop.
react-router match and props
import React, {Component} from 'react'
import {Link} from 'react-router-dom'
import Bus from './bus'
const busses = (match, busses) => busses.map((bus, index) => (
<Link key={index} to={`${match.path}/${bus.VEHICLE}`}>
<Bus
busNumber={bus.VEHICLE}
busRoute={bus.ROUTE}
nextStop={bus.TIMEPOINT}/>
</Link>
)
)
const AllBusses = ({match, ...props}) => (
<div> {busses(match, props.busses)} </div>
)
export default AllBusses
import React, {Component} from 'react';
import {Switch, Route} from 'react-router-dom';
import AllBusses from './allBusses';
import OneBus from './oneBus';
const proxyUrl = 'https://cors-anywhere.herokuapp.com/'
export default class BusContainer extends Component {
constructor(){
super()
this.state = {
busses: []
}
}
componentDidMount = () => {
const targetUrl = 'http://developer.itsmarta.com/BRDRestService/RestBusRealTimeService/GetAllBus'
fetch(proxyUrl + targetUrl)
.then(response => response.json())
.then(busses => this.setState({busses}))
}
render(){
const {busses} = this.state
return(
<div>
<Switch>
<Route path ='/busses/:busId' render={(props) => <OneBus {...props}/> }/>
<Route path='/busses' render={(props) => (
<AllBusses {...props} busses={busses}/>
)}/>
</Switch>
</div>
)
}
}
@shanebarringer
Copy link
Author

Okay, awesome! That really clears things up for me. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment