https://github.com/reactjs/react-router/blob/master/docs/API.md
Route
und IndexRoute
children
-Property
location
und param
Property und componentWillReceiveProps
import { Router, Route, Redirect, IndexRoute, hashHistory }
from 'react-router';
const routes = <Router history={hashHistory}>
<Redirect from="/vote" to="/votes"/>
<Route path="/" component={Layout}>
<IndexRoute component={VotePage}/>
<Route path="votes/:id" component={SingleVotePage}/>
<Route path="login(/:redirect)" component={LoginPage}/>
<Route path="compose" component={VoteComposerPage} onEnter={requireAuth}/>
<Route path="*" component={NoMatchPage}/>
</Route>
</Router>;
const routes = <Router history={hashHistory}>
...
</Router>;
hashHistory
codiert Pfad in angehängten Hash (#/vote)
browserHistory
codiert Pfad direkt in URL (/vote)
// Push a new entry onto the history stack.
history.push('/home')
// Replace the current entry on the history stack.
history.replace('/profile')
// plain match (containing nested routes)
<Route path="/" component={Layout}>
// id is passed as parameter to component
<Route path="votes/:id" component={SingleVotePage}/> /
// redirect is optional
<Route path="login(/:redirect)" component={LoginPage}/>
// onEnter and onExit possible as hooks
<Route path="compose" component={VoteComposerPage}
onEnter={requireAuth}/>
// wildcard
<Route path="*" component={NoMatchPage}/>
</Route>
// /vote redirects to /votes
<Redirect from="/vote" to="/votes"/>
<Route path="/" component={Layout}>
// will be rendered additionally when path is just /
<IndexRoute component={VotePage}/>
// ...
</Route>
children
in den props
übergeben
children
kann grundsätzlich eine oder mehrere Komponenten sein
export default function Layout(props) {
return
Greetings
{props.children}
;
}
a
-Element gerendert
<Link to={`/votes/${vote.id}`} activeClassName='active'>
{vote.title}
</Link>
Der Router übergibt den zu rendernden Routen Daten als Properties
location
: enthält pathname
und query
params
: die dynamischen Segmente der Route (z.B. id
)componentWillReceiveProps
: wird mit neuen Properties aufgerufen, wenn sich die Route geändert hat
code/material/router
in deinen src-Ordner
GreetingMaster
enthält Links auf darzustellende Grüße
main.js
eine Route auf GreetingDisplay
hinzu
GreetingDisplay
so dass sie einen solchen Gruß darstellt mit den vom Router übergebenen Parametern ausgibt