forked from remix-run/react-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStaticRouter.js
More file actions
65 lines (59 loc) · 1.84 KB
/
StaticRouter.js
File metadata and controls
65 lines (59 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import React, { Component } from "react";
import { StaticRouter as Router, Route } from "react-router-dom";
// This example renders a route within a StaticRouter and populates its
// staticContext, which it then prints out. In the real world you would
// use the StaticRouter for server-side rendering:
//
// import express from "express";
// import ReactDOMServer from "react-dom/server";
//
// const app = express()
//
// app.get('*', (req, res) => {
// let staticContext = {}
//
// let html = ReactDOMServer.renderToString(
// <StaticRouter location={req.url} context={staticContext}>
// <App /> (includes the RouteStatus component below e.g. for 404 errors)
// </StaticRouter>
// );
//
// res.status(staticContext.statusCode || 200).send(html);
// });
//
// app.listen(process.env.PORT || 3000);
function RouteStatus(props) {
return (
<Route
render={({ staticContext }) => {
// we have to check if staticContext exists
// because it will be undefined if rendered through a BrowserRouter
if (staticContext) {
staticContext.statusCode = props.statusCode;
}
return <div>{props.children}</div>;
}}
/>
);
}
function PrintContext(props) {
return <p>Static context: {JSON.stringify(props.staticContext)}</p>;
}
export default class StaticRouterExample extends Component {
// This is the context object that we pass to the StaticRouter.
// It can be modified by routes to provide additional information
// for the server-side render
staticContext = {};
render() {
return (
<Router location="/foo" context={this.staticContext}>
<div>
<RouteStatus statusCode={404}>
<p>Route with statusCode 404</p>
<PrintContext staticContext={this.staticContext} />
</RouteStatus>
</div>
</Router>
);
}
}