Modify the address bar with history.pushState
Describe how bookmarking a single page application works
history.back();
history.forward();
history.pushState({}, 'title', '/newpage');
Server Side Rendering
GET /
index.html (fully rendered)
GET /signin
/signin (fully rendered)
Node.js
HTML
GET /
index.html (only div id="root")
Node.js
React
Render react app
Change address to /signin
Render /signin component
GET /user/55
index.html (only div id="root")
Node.js
React
Render react app with
correct user component
Bookmarked /user/55
Server side support is required
Describe React Router v4
Differentiate BrowserRouter vs HashRouter
Use Link, Switch, and Route components
import React from 'react';
import ReactDOM from 'react-dom';
import {
BrowserRouter as Router
} from 'react-router-dom';
import App from './App';
ReactDOM.render(
<Router>
<App />
</Router>,
document.getElementById('root')
);
import React, {Component} from 'react';
import {
Switch, Route
} from 'react-router-dom';
const Homepage = () => (<div>HOMEPAGE</div>);
const About = () => (<div>ABOUT</div>);
const SwitchDemo = () => (
<Switch>
<Route path="/about" component={About}/>
<Route path="/" component={Homepage}/>
</Switch>
);
Link
import React from 'react';
import {Link} from 'react-router-dom';
import SwitchDemo from './SwitchDemo';
const App = () => (
<div>
<Link to="/">HOME</Link>
<Link to="/about">ABOUT</Link>
<div style={{fontSize: '3em',
margin: '25px'}}>
<SwitchDemo/>
</div>
</div>
);
NavLink
import React from 'react';
import {NavLink} from 'react-router-dom';
import SwitchDemo from './SwitchDemo';
const s={color: "red"}; //active style
const App = () => (
<div>
<NavLink exact activeStyle={s} to="/">
HOME
</NavLink>
<NavLink exact activeStyle={s} to="/about">
ABOUT
</NavLink>
<div style={{fontSize: '3em',margin: '25px'}}>
<SwitchDemo/>
</div>
</div>
);
Use URL parameters for a Route
Define Route props
Define withRouter
Passing your own props to a component in Route (render vs component)
URL Parameters
import React from 'react';
import {
Switch, Route
} from 'react-router-dom';
const Homepage = () => (<div>HOMEPAGE</div>);
const Name = ({match}) => (
<div>Hello, {match.params.name}</div>
);
const SwitchDemo = () => (
<Switch>
<Route path="/:name" component={Name}/>
<Route path="/" component={Homepage}/>
</Switch>
);
match - info about how the url matches the route component
location - where you are now, similar to window.location
history - similar to html5 history object, allows explicit changes to the url
import {
withRouter, Switch, Route
} from 'react-router-dom';
const SwitchDemo = ({history}) => (
<div>
<Switch>
<Route path="/:name" component={Name}/>
<Route path="/" component={Homepage}/>
</Switch>
<button onClick={() => history.push('/')}>
Go Home
</button>
</div>
);
export default withRouter(SwitchDemo);
import {Route} from 'react-router-dom';
const teachers = ['Tim', 'Colt', 'Matt', 'Elie'];
const Teachers = ({teachers}) => (
<ul>
{teachers.map((teach, ind) => (
<li key={i}>{teach}</li>
))}
</ul>
);
const App = () => (
<Route path="/teachers" render={props => (
<Teachers {...props} teachers={teachers} />
)}/>
);