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

On line 15 of AllBusses is there a better way to work with additional props (outside of what match offers?)

@ryanflorence
Copy link

Nope, that's perfect. BusContainer owns the bus state, passes it down to child components, they just happen to be assigned a URL with Route, but react router isn't trying to change anything about your normal React data flow.

@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