Skip to content

Commit 582654a

Browse files
committed
add Hydration test
1 parent 4285fd5 commit 582654a

File tree

4 files changed

+210
-6
lines changed

4 files changed

+210
-6
lines changed

dist/bundle.js

Lines changed: 190 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"main": "index.js",
66
"scripts": {
77
"start": "babel-node server.js",
8+
"watch": "npx webpack --watch",
89
"test": "echo \"Error: no test specified\" && exit 1"
910
},
1011
"keywords": [],

server.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
// server.js
21
const express = require('express');
32
const React = require('react');
43
const ReactDOMServer = require('react-dom/server');
4+
const path = require('path');
55
const App = require('./src/App').default;
66

77
const app = express();
88

9+
// 提供打包后的静态文件
10+
app.use(express.static(path.resolve(__dirname, 'dist')));
11+
912
app.get('*', (req, res) => {
1013
const appHtml = ReactDOMServer.renderToString(React.createElement(App));
1114
const html = `
@@ -28,4 +31,3 @@ app.get('*', (req, res) => {
2831
app.listen(3000, () => {
2932
console.log('Server is running on http://localhost:3000');
3033
});
31-

src/index.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1-
// src/index.js
2-
import React from 'react';
1+
import React, { useState } from 'react';
32
import { hydrateRoot } from 'react-dom/client';
43
import App from './App';
54

6-
// 使用 hydrateRoot 方法补水
7-
hydrateRoot(document.getElementById('root'), <App />);
5+
function HydrationTest() {
6+
const [clicked, setClicked] = useState(false);
7+
8+
return (
9+
<div>
10+
<App />
11+
<button onClick={() => setClicked(!clicked)}>
12+
{clicked ? 'Clicked!' : 'Click me'}
13+
</button>
14+
</div>
15+
);
16+
}
17+
18+
hydrateRoot(document.getElementById('root'), <HydrationTest />);

0 commit comments

Comments
 (0)