Skip to content

Commit 9848ac9

Browse files
author
Thomas Roger Lux
committed
Add react router
1 parent 6716df4 commit 9848ac9

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

Diff for: package.json

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"react": "^16.4.1",
1414
"react-dom": "^16.4.1",
1515
"react-redux": "^5.0.7",
16+
"react-router-dom": "^4.3.1",
1617
"redux": "^4.0.0",
1718
"styled-components": "^3.4.9"
1819
},
@@ -21,6 +22,7 @@
2122
"@types/react": "^16.4.1",
2223
"@types/react-dom": "^16.0.6",
2324
"@types/react-redux": "^6.0.2",
25+
"@types/react-router-dom": "^4.3.1",
2426
"ts-loader": "^4.4.1",
2527
"typescript": "^2.9.2",
2628
"webpack": "^4.12.1",

Diff for: src/client/index.tsx

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,23 @@ import * as React from "react";
22
import * as ReactDOM from "react-dom";
33
import * as Redux from "redux";
44
import * as ReactRedux from "react-redux";
5+
import { BrowserRouter as Router } from "react-router-dom";
56

67
import App from "common/App";
78
import { changeTitle } from "common/redux/reducer/title";
89

910
//@ts-ignore
10-
const preloadedState = window["PRELOADED_STATE"];
11+
const preloadedState = window["__PRELOADED_STATE__"];
1112
//@ts-ignore
12-
delete window["PRELOADED_STATE"];
13+
delete window["__PRELOADED_STATE__"];
1314

1415
const store = Redux.createStore(changeTitle, preloadedState)
1516

1617
ReactDOM.hydrate(
1718
<ReactRedux.Provider store={store}>
18-
<App />
19+
<Router>
20+
<App />
21+
</Router>
1922
</ReactRedux.Provider>,
2023
document.getElementById("root")
2124
);

Diff for: src/server/index.tsx

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as Express from "express";
44
import * as Redux from "redux";
55
import * as ReactRedux from "react-redux";
66
import { ServerStyleSheet } from "styled-components";
7+
import { StaticRouter as Router } from "react-router-dom";
78

89
import App from "common/App";
910
import { changeTitle } from "common/redux/reducer/title";
@@ -16,12 +17,14 @@ function main() {
1617

1718
express.use(Express.static("build"));
1819

19-
express.get("/", (req, res, next) => {
20+
express.get("/*", (req, res, next) => {
2021

2122
const sheet = new ServerStyleSheet()
2223
const html = ReactDOM.renderToString(sheet.collectStyles(
2324
<ReactRedux.Provider store={store}>
24-
<App />
25+
<Router location={req.path} context={{}}>
26+
<App />
27+
</Router>
2528
</ReactRedux.Provider>
2629
));
2730

@@ -39,7 +42,7 @@ function main() {
3942
<body>
4043
<div id="root">${html}</div>
4144
<script>
42-
window["PRELOADED_STATE"] = ${preloadedState}
45+
window["__PRELOADED_STATE__"] = ${preloadedState}
4346
</script>
4447
<script type="application/javascript" src="bundle.js"></script>
4548
${styleTags}

0 commit comments

Comments
 (0)