A single-page application (SPA) is a web application or web site that interacts with the user by dynamically rewriting the current page rather than loading entire new pages from a server.
Server-side rendering (SSR) is a popular technique for rendering a normally client-side only single page app (SPA) on the server and then sending a fully rendered page to the client. The client’s JavaScript bundle can then take over and the SPA can operate as normal.
and many more...
Declarative routing for React
Imperative programming is a programming paradigm that uses statements that change a program’s state.
Declarative programming is a programming paradigm … that expresses the logic of a computation without describing its control flow.
if (route === routeA)...
else if (route === routeB)...
<RouteA />
<RouteB />
define router
import React from 'react'
import ReactDOM from 'react-dom'
import { BrowserRouter as Router } from 'react-router-dom'
ReactDOM.render(
<Router>
<div>
<!-- -->
</div>
</Router>,
document.getElementById('app'));
define links
import React from 'react'
import ReactDOM from 'react-dom'
import { BrowserRouter as Router, Link } from 'react-router-dom'
ReactDOM.render(
<Router>
<div>
<aside>
<Link to='/dashboard'>Dashboard</Link>
<Link to='/about'>About</Link>
</aside>
</div>
</Router>,
document.getElementById('app'));
router types
define components
const Dashboard = () => (
<div>
<h2>Dashboard</h2>
...
</div>)
const About = () => (
<div>
<h2>About</h2>
...
</div>)
use components
<Router>
<aside>
<Link to={`/`}>Dashboard</Link>
<Link to={`/about`}>About</Link>
</aside>
<Route exact path="/">
<Dashboard></Dashboard>
</Route>
<Route path="/about">
<About></About>
</Route>
</Router>
match dynamic parameter
const Post = () => {
const { id } = useParams();
// or
const matchedId = useRouteMatch("/post/:id");
return (
<div>
<h2>Post #{id} or {matchedId}</h2>
...
</div>);
}
//...
<Route exact path="/post/:id">
<Post></Post>
</Route>
exact matching
<Switch>
<Route path="/">
<Dashboard></Dashboard>
</Route>
<Route path="/about">
<About></About>
</Route>
</Switch>
using StaticRouter
// client
<BrowserRouter>
<App/>
</BrowserRouter>
// server
<StaticRouter
location={req.url}
context={context}
>
<App/>
</StaticRouter>