implement react-router
This commit is contained in:
parent
d7e9854e70
commit
fb64977d06
11
src/client/Routes.js
Normal file
11
src/client/Routes.js
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import React from "react";
|
||||||
|
import { Route } from "react-router-dom";
|
||||||
|
import Home from "./components/Home";
|
||||||
|
|
||||||
|
export default () => {
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<Route exact path="/" component={Home} />
|
||||||
|
</div>
|
||||||
|
) ;
|
||||||
|
};
|
@ -1,6 +1,11 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import ReactDOM from 'react-dom';
|
import ReactDOM from 'react-dom';
|
||||||
|
import { BrowserRouter } from "react-router-dom";
|
||||||
|
import Routes from './Routes';
|
||||||
|
|
||||||
import Home from './components/Home';
|
ReactDOM.hydrate(
|
||||||
|
<BrowserRouter>
|
||||||
ReactDOM.hydrate(<Home />, document.querySelector('#root'));
|
<Routes />
|
||||||
|
</BrowserRouter>,
|
||||||
|
document.querySelector('#root')
|
||||||
|
);
|
@ -1,9 +1,14 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import { renderToString } from "react-dom/server";
|
import { renderToString } from "react-dom/server";
|
||||||
import Home from "../client/components/Home";
|
import { StaticRouter } from "react-router-dom";
|
||||||
|
import Routes from "../client/Routes";
|
||||||
|
|
||||||
export default () => {
|
export default (req) => {
|
||||||
const content = renderToString(<Home />);
|
const content = renderToString(
|
||||||
|
<StaticRouter location={req.path} context={{}}>
|
||||||
|
<Routes />
|
||||||
|
</StaticRouter>
|
||||||
|
);
|
||||||
|
|
||||||
return `
|
return `
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
|
@ -5,7 +5,7 @@ const app = express();
|
|||||||
|
|
||||||
app.use(express.static('public'));
|
app.use(express.static('public'));
|
||||||
app.get('/', (req, res) => {
|
app.get('/', (req, res) => {
|
||||||
res.send(renderer());
|
res.send(renderer(req));
|
||||||
});
|
});
|
||||||
|
|
||||||
app.listen(3000, () => {
|
app.listen(3000, () => {
|
||||||
|
Loading…
Reference in New Issue
Block a user