Created
September 6, 2017 16:33
-
-
Save shanebarringer/79b38975284e4dde3101d75572f8c57c to your computer and use it in GitHub Desktop.
react-router match and props
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | |
) | |
} | |
} |
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
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.