feat: 新增 node-frontend-react-class 示例(React 19 类组件,Tengine 托管)
This commit is contained in:
@@ -0,0 +1,4 @@
|
|||||||
|
dist/
|
||||||
|
node_modules/
|
||||||
|
.git/
|
||||||
|
.DS_Store
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
dist/
|
||||||
|
node_modules/
|
||||||
|
npm-debug.log*
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
# 编译阶段:compile-node22(Vite 7 需要 Node >= 20.19)
|
||||||
|
FROM registry.cn-shanghai.aliyuncs.com/odboy/kenaito:compile-node22 AS build
|
||||||
|
WORKDIR /src
|
||||||
|
|
||||||
|
COPY package.json package-lock.json* ./
|
||||||
|
# npm 源由镜像的 /usr/local/node/etc/npmrc 指向 npmmirror,无需额外换源
|
||||||
|
RUN npm install --no-audit --no-fund
|
||||||
|
|
||||||
|
COPY . ./
|
||||||
|
RUN npm run build && ls -lh dist/ dist/assets/
|
||||||
|
|
||||||
|
# 运行阶段:Tengine 3.1.0(自建 proxy-tengine3 镜像)托管静态文件
|
||||||
|
FROM registry.cn-shanghai.aliyuncs.com/odboy/kenaito:proxy-tengine3 AS runtime
|
||||||
|
LABEL MAINTAINER="tianjun@odboy.cn"
|
||||||
|
ENV TimeZone=Asia/Shanghai
|
||||||
|
# 站点配置与构建产物都放进镜像:conf.d/*.conf 由主配置 include,html/ 为站点根目录
|
||||||
|
COPY nginx/node-frontend-react-class.conf /usr/local/nginx/conf/conf.d/node-frontend-react-class.conf
|
||||||
|
COPY --from=build /src/dist/ /usr/local/nginx/html/
|
||||||
|
# 构建期自检:配置语法错误直接让镜像构建失败;纯静态站点没有上游主机名,nginx -t 可直接通过
|
||||||
|
RUN /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
|
||||||
|
EXPOSE 80
|
||||||
|
STOPSIGNAL SIGQUIT
|
||||||
|
CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
# node-frontend-react-class 示例
|
||||||
|
|
||||||
|
React 19 **类组件**(class component)写法的前端项目,**构建阶段**用 `compile-node22` 跑 Vite,**运行阶段**以自建
|
||||||
|
`proxy-tengine3` 镜像为基座托管静态文件。函数组件 + Hooks 写法见同级的
|
||||||
|
[`node-frontend-react-function`](../node-frontend-react-function/README.md),两者页面与部署方式完全一致,只差组件写法。
|
||||||
|
|
||||||
|
| 阶段 | 镜像 | 说明 |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| 编译 | `registry.cn-shanghai.aliyuncs.com/odboy/kenaito:compile-node22` | Node 22 + npm(源为 npmmirror),Vite 7 要求 Node ≥ 20.19 |
|
||||||
|
| 运行 | `registry.cn-shanghai.aliyuncs.com/odboy/kenaito:proxy-tengine3` | Tengine 3.1.0,托管 `dist/` |
|
||||||
|
|
||||||
|
纯前端项目:页面数据全部在浏览器内产生,不调用任何后端接口,部署只需要一个 Tengine 容器。
|
||||||
|
|
||||||
|
### 示例内容
|
||||||
|
|
||||||
|
```
|
||||||
|
node-frontend-react-class/
|
||||||
|
├── package.json # react 19.3.1 / react-dom 19.3.1;vite 7 + plugin-react 5
|
||||||
|
├── vite.config.js # base: './';react/react-dom 单独拆包
|
||||||
|
├── index.html
|
||||||
|
├── src/
|
||||||
|
│ ├── main.jsx # createRoot + StrictMode
|
||||||
|
│ ├── App.jsx # class App extends Component,this.state 切换页签
|
||||||
|
│ ├── styles.css # 轻量手写样式,不引 UI 库
|
||||||
|
│ └── views/
|
||||||
|
│ ├── Home.jsx # this.state + componentDidMount/WillUnmount:计数器、时钟、受控输入
|
||||||
|
│ ├── Form.jsx # 类组件受控表单 + validate,错误/结果放 this.state
|
||||||
|
│ └── About.jsx # 版本与部署说明(无状态类组件)
|
||||||
|
├── nginx/
|
||||||
|
│ └── node-frontend-react-class.conf # Tengine 站点配置:静态托管 + /healthz
|
||||||
|
├── Dockerfile # 两阶段:compile-node22 -> proxy-tengine3
|
||||||
|
├── build.sh # 构建镜像(PUSH=1 时同时推送)
|
||||||
|
├── docker-compose.yml # 只部署前端(端口 8093)
|
||||||
|
├── startup.sh / shutdown.sh
|
||||||
|
└── .dockerignore / .gitignore
|
||||||
|
```
|
||||||
|
|
||||||
|
### 构建与部署
|
||||||
|
|
||||||
|
```shell
|
||||||
|
sh build.sh # docker build -t registry.cn-shanghai.aliyuncs.com/odboy/kenaito:demo-node-frontend-react-class .
|
||||||
|
PUSH=1 sh build.sh # 构建并推送
|
||||||
|
sh startup.sh # docker-compose -f docker-compose.yml -p node-frontend-react-class-demo up -d
|
||||||
|
```
|
||||||
|
|
||||||
|
访问 http://127.0.0.1:8093/ ;`curl http://127.0.0.1:8093/healthz` 返回 `ok`。
|
||||||
|
|
||||||
|
### 本地开发(不用 docker)
|
||||||
|
|
||||||
|
```shell
|
||||||
|
npm install
|
||||||
|
npm run dev # http://127.0.0.1:5573
|
||||||
|
```
|
||||||
|
|
||||||
|
### 关键点
|
||||||
|
|
||||||
|
**类组件写法**:组件继承 `Component`,状态放 `this.state`、更新用 `this.setState`;定时器在
|
||||||
|
`componentDidMount` 里创建、`componentWillUnmount` 里清理(对应函数版的 `useEffect` cleanup)。
|
||||||
|
事件处理函数在构造函数里 `bind(this)`,或用箭头函数保持 `this`。
|
||||||
|
|
||||||
|
**函数版 vs 类版**:这是官方文档并列的两种写法,行为等价。新代码建议用函数组件 + Hooks
|
||||||
|
(见 `node-frontend-react-function`);类组件在旧项目、错误边界(`componentDidCatch`)里仍会遇到。
|
||||||
|
|
||||||
|
**站点配置**:配置放在 `nginx/node-frontend-react-class.conf`,构建时 `COPY` 到 `/usr/local/nginx/conf/conf.d/`
|
||||||
|
(镜像自带 nginx.conf 已经 `include conf.d/*.conf`);compose 又把宿主机 `./nginx` 挂到同一个 `conf.d`。
|
||||||
|
挂目录而不是单文件:单文件挂载编辑会换 inode,容器内看不到改动。
|
||||||
|
|
||||||
|
### 停止
|
||||||
|
|
||||||
|
```shell
|
||||||
|
sh shutdown.sh
|
||||||
|
```
|
||||||
Executable
+11
@@ -0,0 +1,11 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# 在示例目录下构建示例镜像(编译 + 运行两阶段均在本机 docker 内完成)
|
||||||
|
set -euo pipefail
|
||||||
|
cd "$(dirname "$0")"
|
||||||
|
|
||||||
|
IMAGE=${IMAGE:-registry.cn-shanghai.aliyuncs.com/odboy/kenaito:demo-node-frontend-react-class}
|
||||||
|
docker build -t "${IMAGE}" .
|
||||||
|
|
||||||
|
if [ "${PUSH:-0}" = "1" ]; then
|
||||||
|
docker push "${IMAGE}"
|
||||||
|
fi
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
version: '3.8'
|
||||||
|
|
||||||
|
# 网段避开其它示例(172.31.0/24 ~ 172.31.6/24 已被 go / rust / node-backend / vue2 / vue3 / react-function / vite5 占用)
|
||||||
|
networks:
|
||||||
|
node-frontend-react-class-demo:
|
||||||
|
ipam:
|
||||||
|
driver: default
|
||||||
|
config:
|
||||||
|
- subnet: "172.31.7.0/24"
|
||||||
|
|
||||||
|
services:
|
||||||
|
frontend:
|
||||||
|
image: registry.cn-shanghai.aliyuncs.com/odboy/kenaito:demo-node-frontend-react-class # 镜像由 build.sh 构建
|
||||||
|
container_name: node-frontend-react-class-demo
|
||||||
|
restart: unless-stopped
|
||||||
|
ports: # 映射端口:前端页面
|
||||||
|
- "8093:80"
|
||||||
|
volumes: # 只挂载站点配置目录,改完 reload 即可,不必重建镜像
|
||||||
|
# 挂目录而不是单文件:单文件挂载在宿主机上编辑会换 inode,容器内看不到改动
|
||||||
|
- "./nginx:/usr/local/nginx/conf/conf.d"
|
||||||
|
environment: # 设置环境变量,相当于docker run命令中的-e
|
||||||
|
TZ: Asia/Shanghai
|
||||||
|
healthcheck:
|
||||||
|
# 运行镜像自带 net-tools,直接用端口探测
|
||||||
|
test: ["CMD-SHELL", "netstat -tln | grep -q ':80 '"]
|
||||||
|
interval: 30s
|
||||||
|
timeout: 5s
|
||||||
|
retries: 3
|
||||||
|
start_period: 5s
|
||||||
|
networks:
|
||||||
|
node-frontend-react-class-demo:
|
||||||
|
ipv4_address: 172.31.7.20
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="zh-CN">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>node-frontend-react-class | React 19 类组件</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="root"></div>
|
||||||
|
<script src="/src/main.jsx" type="module"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
# node-frontend-react-class 站点:Tengine 3.1.0 托管 React 构建产物(纯静态,无后端依赖)
|
||||||
|
server {
|
||||||
|
listen 80;
|
||||||
|
server_name _;
|
||||||
|
|
||||||
|
access_log /dev/stdout main;
|
||||||
|
|
||||||
|
root /usr/local/nginx/html;
|
||||||
|
index index.html;
|
||||||
|
|
||||||
|
# 静态资源带 hash,长缓存;index.html 每次重新验证,保证发版后立即生效
|
||||||
|
location /assets/ {
|
||||||
|
expires 30d;
|
||||||
|
add_header Cache-Control "public, immutable";
|
||||||
|
try_files $uri =404;
|
||||||
|
}
|
||||||
|
|
||||||
|
# 前端路由兜底:示例不引入路由库,保留是为了以后加 react-router 时不用改配置
|
||||||
|
location / {
|
||||||
|
try_files $uri $uri/ /index.html;
|
||||||
|
}
|
||||||
|
|
||||||
|
# 健康检查:直接读 index.html
|
||||||
|
location = /healthz {
|
||||||
|
access_log off;
|
||||||
|
default_type text/plain;
|
||||||
|
return 200 "ok\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
{
|
||||||
|
"name": "node-frontend-react-class",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"private": true,
|
||||||
|
"description": "React 19 前端示例,构建产物由 Tengine 托管",
|
||||||
|
"type": "module",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=20.19"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"dev": "vite",
|
||||||
|
"build": "vite build",
|
||||||
|
"preview": "vite preview --port 4173"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"react": "19.3.1",
|
||||||
|
"react-dom": "19.3.1"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@vitejs/plugin-react": "5.2.0",
|
||||||
|
"vite": "7.3.8"
|
||||||
|
}
|
||||||
|
}
|
||||||
+4
@@ -0,0 +1,4 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
set -euo pipefail
|
||||||
|
cd "$(dirname "$0")"
|
||||||
|
docker-compose -f docker-compose.yml -p node-frontend-react-class-demo down
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
import { Component } from 'react';
|
||||||
|
import Home from './views/Home.jsx';
|
||||||
|
import Form from './views/Form.jsx';
|
||||||
|
import About from './views/About.jsx';
|
||||||
|
|
||||||
|
const TABS = [
|
||||||
|
{ key: 'home', label: '首页', view: Home },
|
||||||
|
{ key: 'form', label: '表单', view: Form },
|
||||||
|
{ key: 'about', label: '关于', view: About },
|
||||||
|
];
|
||||||
|
|
||||||
|
// 类组件写法:用 this.state + this.setState 管理页签,等价于函数版的 useState
|
||||||
|
export default class App extends Component {
|
||||||
|
constructor(props) {
|
||||||
|
super(props);
|
||||||
|
this.state = { active: 'home' };
|
||||||
|
this.setActive = this.setActive.bind(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
setActive(key) {
|
||||||
|
this.setState({ active: key });
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
const { active } = this.state;
|
||||||
|
const Current = TABS.find((t) => t.key === active).view;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="app">
|
||||||
|
<header className="header">
|
||||||
|
<span className="logo">node-frontend-react-class</span>
|
||||||
|
<nav className="nav">
|
||||||
|
{TABS.map((t) => (
|
||||||
|
<button
|
||||||
|
key={t.key}
|
||||||
|
type="button"
|
||||||
|
className={t.key === active ? 'nav-item active' : 'nav-item'}
|
||||||
|
onClick={() => this.setActive(t.key)}
|
||||||
|
>
|
||||||
|
{t.label}
|
||||||
|
</button>
|
||||||
|
))}
|
||||||
|
</nav>
|
||||||
|
</header>
|
||||||
|
<main className="main">
|
||||||
|
<Current />
|
||||||
|
</main>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
import { StrictMode } from 'react';
|
||||||
|
import { createRoot } from 'react-dom/client';
|
||||||
|
import App from './App.jsx';
|
||||||
|
import './styles.css';
|
||||||
|
|
||||||
|
createRoot(document.getElementById('root')).render(
|
||||||
|
<StrictMode>
|
||||||
|
<App />
|
||||||
|
</StrictMode>,
|
||||||
|
);
|
||||||
@@ -0,0 +1,156 @@
|
|||||||
|
* {
|
||||||
|
box-sizing: border-box;
|
||||||
|
}
|
||||||
|
|
||||||
|
html,
|
||||||
|
body,
|
||||||
|
#root {
|
||||||
|
height: 100%;
|
||||||
|
margin: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
body {
|
||||||
|
font-family: -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
|
||||||
|
color: #303133;
|
||||||
|
background: #f5f7fa;
|
||||||
|
}
|
||||||
|
|
||||||
|
.header {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 24px;
|
||||||
|
height: 56px;
|
||||||
|
padding: 0 20px;
|
||||||
|
background: #fff;
|
||||||
|
border-bottom: 1px solid #ebeef5;
|
||||||
|
}
|
||||||
|
|
||||||
|
.logo {
|
||||||
|
font-size: 18px;
|
||||||
|
font-weight: 600;
|
||||||
|
color: #409eff;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nav {
|
||||||
|
display: flex;
|
||||||
|
gap: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nav-item {
|
||||||
|
padding: 6px 14px;
|
||||||
|
border: 1px solid transparent;
|
||||||
|
border-radius: 4px;
|
||||||
|
background: transparent;
|
||||||
|
color: #303133;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nav-item:hover {
|
||||||
|
color: #409eff;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nav-item.active {
|
||||||
|
color: #409eff;
|
||||||
|
border-color: #d9ecff;
|
||||||
|
background: #ecf5ff;
|
||||||
|
}
|
||||||
|
|
||||||
|
.main {
|
||||||
|
padding: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.card {
|
||||||
|
background: #fff;
|
||||||
|
border: 1px solid #ebeef5;
|
||||||
|
border-radius: 4px;
|
||||||
|
padding: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.card + .card {
|
||||||
|
margin-top: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.card-title {
|
||||||
|
margin: 0 0 16px;
|
||||||
|
font-size: 16px;
|
||||||
|
font-weight: 600;
|
||||||
|
}
|
||||||
|
|
||||||
|
.grid {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
|
||||||
|
gap: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.kv {
|
||||||
|
width: 100%;
|
||||||
|
border-collapse: collapse;
|
||||||
|
}
|
||||||
|
|
||||||
|
.kv th,
|
||||||
|
.kv td {
|
||||||
|
padding: 6px 12px;
|
||||||
|
border: 1px solid #ebeef5;
|
||||||
|
text-align: left;
|
||||||
|
}
|
||||||
|
|
||||||
|
.kv th {
|
||||||
|
width: 40%;
|
||||||
|
background: #fafafa;
|
||||||
|
}
|
||||||
|
|
||||||
|
.clock {
|
||||||
|
margin: 0 0 12px;
|
||||||
|
font-size: 24px;
|
||||||
|
font-weight: 600;
|
||||||
|
font-variant-numeric: tabular-nums;
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn {
|
||||||
|
padding: 6px 14px;
|
||||||
|
border: 1px solid #dcdfe6;
|
||||||
|
border-radius: 4px;
|
||||||
|
background: #fff;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn.primary {
|
||||||
|
color: #fff;
|
||||||
|
border-color: #409eff;
|
||||||
|
background: #409eff;
|
||||||
|
}
|
||||||
|
|
||||||
|
.field {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 12px;
|
||||||
|
margin-bottom: 14px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.field label {
|
||||||
|
width: 80px;
|
||||||
|
text-align: right;
|
||||||
|
}
|
||||||
|
|
||||||
|
.field input,
|
||||||
|
.field select {
|
||||||
|
flex: 1;
|
||||||
|
max-width: 320px;
|
||||||
|
padding: 6px 10px;
|
||||||
|
border: 1px solid #dcdfe6;
|
||||||
|
border-radius: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.hint {
|
||||||
|
margin-top: 12px;
|
||||||
|
padding: 8px 12px;
|
||||||
|
border-radius: 4px;
|
||||||
|
background: #f0f9eb;
|
||||||
|
color: #67c23a;
|
||||||
|
}
|
||||||
|
|
||||||
|
.error {
|
||||||
|
margin-left: 8px;
|
||||||
|
color: #f56c6c;
|
||||||
|
font-size: 12px;
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
import { Component } from 'react';
|
||||||
|
|
||||||
|
// 静态说明页:类组件写法,无状态
|
||||||
|
export default class About extends Component {
|
||||||
|
render() {
|
||||||
|
return (
|
||||||
|
<div className="card">
|
||||||
|
<h3 className="card-title">关于</h3>
|
||||||
|
<table className="kv">
|
||||||
|
<tbody>
|
||||||
|
<tr><th>框架</th><td>React 19.3.1(class 组件)</td></tr>
|
||||||
|
<tr><th>写法</th><td>继承 Component,状态在 this.state,生命周期用 componentDidMount/WillUnmount</td></tr>
|
||||||
|
<tr><th>构建</th><td>Vite 7 + @vitejs/plugin-react 5</td></tr>
|
||||||
|
<tr><th>部署</th><td>Tengine 3.1.0(proxy-tengine3 镜像)托管 dist/</td></tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
import { Component } from 'react';
|
||||||
|
|
||||||
|
const EMPTY = { name: '', type: '', enabled: true, date: '' };
|
||||||
|
|
||||||
|
// 类组件受控表单:字段统一走 this.state.form,错误与结果各占一个 state 字段
|
||||||
|
export default class Form extends Component {
|
||||||
|
constructor(props) {
|
||||||
|
super(props);
|
||||||
|
this.state = { form: { ...EMPTY }, errors: {}, result: '' };
|
||||||
|
this.update = this.update.bind(this);
|
||||||
|
this.submit = this.submit.bind(this);
|
||||||
|
this.reset = this.reset.bind(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
update(key, value) {
|
||||||
|
this.setState((s) => ({ form: { ...s.form, [key]: value } }));
|
||||||
|
}
|
||||||
|
|
||||||
|
validate(form) {
|
||||||
|
const next = {};
|
||||||
|
if (!form.name.trim()) next.name = '请输入名称';
|
||||||
|
if (!form.type) next.type = '请选择类型';
|
||||||
|
this.setState({ errors: next });
|
||||||
|
return Object.keys(next).length === 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
submit(e) {
|
||||||
|
e.preventDefault();
|
||||||
|
const { form } = this.state;
|
||||||
|
if (!this.validate(form)) return;
|
||||||
|
this.setState({ result: `已提交:${JSON.stringify(form)}` });
|
||||||
|
}
|
||||||
|
|
||||||
|
reset() {
|
||||||
|
this.setState({ form: { ...EMPTY }, errors: {}, result: '' });
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
const { form, errors, result } = this.state;
|
||||||
|
return (
|
||||||
|
<div className="card">
|
||||||
|
<h3 className="card-title">React 类组件受控表单</h3>
|
||||||
|
<form onSubmit={this.submit} noValidate>
|
||||||
|
<div className="field">
|
||||||
|
<label htmlFor="name">名称</label>
|
||||||
|
<input id="name" value={form.name} onChange={(e) => this.update('name', e.target.value)} placeholder="请输入名称" />
|
||||||
|
{errors.name && <span className="error">{errors.name}</span>}
|
||||||
|
</div>
|
||||||
|
<div className="field">
|
||||||
|
<label htmlFor="type">类型</label>
|
||||||
|
<select id="type" value={form.type} onChange={(e) => this.update('type', e.target.value)}>
|
||||||
|
<option value="">请选择</option>
|
||||||
|
<option value="frontend">前端</option>
|
||||||
|
<option value="backend">后端</option>
|
||||||
|
</select>
|
||||||
|
{errors.type && <span className="error">{errors.type}</span>}
|
||||||
|
</div>
|
||||||
|
<div className="field">
|
||||||
|
<label htmlFor="enabled">启用</label>
|
||||||
|
<input id="enabled" type="checkbox" checked={form.enabled} onChange={(e) => this.update('enabled', e.target.checked)} />
|
||||||
|
</div>
|
||||||
|
<div className="field">
|
||||||
|
<label htmlFor="date">日期</label>
|
||||||
|
<input id="date" type="date" value={form.date} onChange={(e) => this.update('date', e.target.value)} />
|
||||||
|
</div>
|
||||||
|
<div className="field">
|
||||||
|
<label />
|
||||||
|
<button type="submit" className="btn primary">提交</button>{' '}
|
||||||
|
<button type="button" className="btn" onClick={this.reset}>重置</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
{result && <div className="hint">{result}</div>}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
import { Component, version as reactVersion } from 'react';
|
||||||
|
|
||||||
|
function format(d) {
|
||||||
|
const p = (n) => String(n).padStart(2, '0');
|
||||||
|
return `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())} ${p(d.getHours())}:${p(d.getMinutes())}:${p(d.getSeconds())}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 类组件:状态放 this.state,用 componentDidMount / componentWillUnmount 管理定时器
|
||||||
|
export default class Home extends Component {
|
||||||
|
constructor(props) {
|
||||||
|
super(props);
|
||||||
|
this.state = {
|
||||||
|
count: 0,
|
||||||
|
clock: format(new Date()),
|
||||||
|
keyword: '',
|
||||||
|
};
|
||||||
|
this.timer = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
componentDidMount() {
|
||||||
|
this.timer = setInterval(() => {
|
||||||
|
this.setState({ clock: format(new Date()) });
|
||||||
|
}, 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
componentWillUnmount() {
|
||||||
|
clearInterval(this.timer);
|
||||||
|
this.timer = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
const { count, clock, keyword } = this.state;
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<div className="grid">
|
||||||
|
<div className="card">
|
||||||
|
<h3 className="card-title">运行时</h3>
|
||||||
|
<table className="kv">
|
||||||
|
<tbody>
|
||||||
|
<tr><th>React</th><td>{reactVersion}</td></tr>
|
||||||
|
<tr><th>写法</th><td>class(继承 Component)</td></tr>
|
||||||
|
<tr><th>路由</th><td>示例用 this.state 页签,不引入路由库</td></tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="card">
|
||||||
|
<h3 className="card-title">计数器(this.state)</h3>
|
||||||
|
<p className="clock">{count}</p>
|
||||||
|
<button type="button" className="btn" onClick={() => this.setState((s) => ({ count: s.count - 1 }))}>-1</button>{' '}
|
||||||
|
<button type="button" className="btn primary" onClick={() => this.setState((s) => ({ count: s.count + 1 }))}>+1</button>{' '}
|
||||||
|
<button type="button" className="btn" onClick={() => this.setState({ count: 0 })}>重置</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="card">
|
||||||
|
<h3 className="card-title">当前时间(componentDidMount)</h3>
|
||||||
|
<p className="clock">{clock}</p>
|
||||||
|
<span className="hint">前端本地时间,不请求后端</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="card">
|
||||||
|
<h3 className="card-title">受控输入</h3>
|
||||||
|
<div className="field">
|
||||||
|
<label htmlFor="kw">关键字</label>
|
||||||
|
<input
|
||||||
|
id="kw"
|
||||||
|
value={keyword}
|
||||||
|
onChange={(e) => this.setState({ keyword: e.target.value })}
|
||||||
|
placeholder="输入关键字过滤,纯前端"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
{keyword && <div className="hint">已输入:{keyword}</div>}
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
set -euo pipefail
|
||||||
|
cd "$(dirname "$0")"
|
||||||
|
docker-compose -f docker-compose.yml -p node-frontend-react-class-demo up -d
|
||||||
|
docker-compose -f docker-compose.yml -p node-frontend-react-class-demo ps
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
import { defineConfig } from 'vite';
|
||||||
|
import react from '@vitejs/plugin-react';
|
||||||
|
|
||||||
|
// 纯静态前端:产物由 Tengine 托管,base 用相对路径,部署到任意子路径都能跑
|
||||||
|
export default defineConfig({
|
||||||
|
plugins: [react()],
|
||||||
|
base: './',
|
||||||
|
server: {
|
||||||
|
host: '0.0.0.0',
|
||||||
|
port: 5573,
|
||||||
|
},
|
||||||
|
build: {
|
||||||
|
outDir: 'dist',
|
||||||
|
sourcemap: false,
|
||||||
|
rollupOptions: {
|
||||||
|
output: {
|
||||||
|
manualChunks: {
|
||||||
|
react: ['react', 'react-dom'],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user