@celljs/react 4.6.1 → 4.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.zh-cn.md +103 -0
- package/package.json +3 -3
package/README.zh-cn.md
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# Cell - React 组件
|
|
2
|
+
|
|
3
|
+
Cell 应用的 React 前端:用装饰器声明的页面、React Router 路由、布局和应用壳,都由前端 DI 容器管理。安装后添加 `frontend` 构建目标(通过 [`@celljs/widget`](../widget))。指南:[前端与 RPC](https://cell.cellbang.com/zh/guide/frontend)。
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install @celljs/react react
|
|
7
|
+
npm install -D @types/react
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
`cell init` 的 `fullstack` 模板已配置它。`cell dev` 用 HMR 在后端端口上为前端提供服务;`cell build` 将其写到 `.cell/output/public/`。
|
|
11
|
+
|
|
12
|
+
## 页面
|
|
13
|
+
|
|
14
|
+
页面是 `src/browser/pages/` 中的文件,每个都以页面组件为默认导出:`index.tsx` 是 `/`、`about.tsx` 是 `/about`、`users/[id].tsx` 是 `/users/:id`、`[...rest].tsx` 匹配路径的剩余部分、`_layout.tsx` 包裹其目录及以下的页面,`_error.tsx` 在其中之一失败时显示。以 `_` 开头的名称和测试不是页面。工具链(`cell dev`、`cell build`)生成路由(`.cell/generated/frontend/routes.js`,由 `cell info --json` 列出);每个页面是访问时加载的块。指南:[前端与 RPC](https://cell.cellbang.com/zh/guide/frontend)。
|
|
15
|
+
|
|
16
|
+
```tsx
|
|
17
|
+
// src/browser/pages/users/[id].tsx
|
|
18
|
+
import { useParams } from '@celljs/react';
|
|
19
|
+
|
|
20
|
+
export default function User() {
|
|
21
|
+
return <h1>User {useParams().id}</h1>;
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
路由使用 React Router 7;`Link`、`NavLink`、`Navigate`、`Outlet`、`useParams`、`useNavigate`、`useLocation`、`useSearchParams` 和 `useMatch` 从这里重新导出。
|
|
26
|
+
|
|
27
|
+
没有页面目录的包通过装饰器在其 DI 模块导入的类上添加页面和替换前端部分:
|
|
28
|
+
|
|
29
|
+
| 装饰器 | |
|
|
30
|
+
| --- | --- |
|
|
31
|
+
| `@View(pathOrMetadata)` / `@Route(...)` | 一个路由。字符串是路径;元数据有 `path`、`index`、`component`(默认:装饰的类)、`layout`、`caseSensitive`、`priority` |
|
|
32
|
+
| `@DefaultLayout(component?)` | 每个没有自己 `layout` 的路由周围的布局(默认:无) |
|
|
33
|
+
| `@App(component?)` | 替换根组件 |
|
|
34
|
+
| `@Router(component?)` | 替换路由 |
|
|
35
|
+
| `@Context(component?)` | 在应用周围添加 React 上下文提供者 |
|
|
36
|
+
|
|
37
|
+
路径相对于 `react.path`(默认 `server.path`,`/`)解析。
|
|
38
|
+
|
|
39
|
+
## 错误
|
|
40
|
+
|
|
41
|
+
渲染时抛出的页面,或代码未加载的页面,不会让应用空白:其布局内的 `ErrorBoundary` 显示备选方案,当用户导航到另一路径时重试页面。备选方案是最近 `_error.tsx` 的默认导出(在页面所在目录或以上),它得到 `{ error, reset }`(`ErrorFallbackProps`);没有时,`DefaultErrorFallback` 显示消息和重试按钮。壳在整个应用周围放另一边界,用于布局和备选方案本身的错误。React 将错误记录到控制台。
|
|
42
|
+
|
|
43
|
+
```tsx
|
|
44
|
+
// src/browser/pages/_error.tsx
|
|
45
|
+
import type { ErrorFallbackProps } from '@celljs/react';
|
|
46
|
+
|
|
47
|
+
export default function PageError({ error, reset }: ErrorFallbackProps) {
|
|
48
|
+
return <p>无法显示此页面({String(error)})。<button onClick={reset}>重试</button></p>;
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
`ErrorBoundary`(`fallback`、`resetKey`)为页面的部分导出。
|
|
53
|
+
|
|
54
|
+
## 组件中的服务
|
|
55
|
+
|
|
56
|
+
| Hook | |
|
|
57
|
+
| --- | --- |
|
|
58
|
+
| `useInject<T>(id)` | 容器的一个组件,每个组件实例解析一次 |
|
|
59
|
+
| `useValue<T>(key, default?)` | 一个配置值,如 `@Value(key)`(前端仅有公开键) |
|
|
60
|
+
| `useContainer()` | 容器本身 |
|
|
61
|
+
| `useRpc<T>(id)` | 后端服务的代理(需要 [`@celljs/rpc`](../../packages/rpc)),如 `@Autorpc(id)` |
|
|
62
|
+
| `useLocale()` | `[locale, setLocale]`:用户选择的 `widget.locales` 的区域设置,以及如何改变它 |
|
|
63
|
+
|
|
64
|
+
它们使用 `ContainerContext` 的容器,壳提供;没有全局容器,所以在应用外渲染的组件失败,消息说如此。在测试中,在 `<ContainerContext.Provider value={container}>` 内渲染组件;`reactTestModule`(`@celljs/react/lib/browser/test/test-module`)有这个包的组件,不需要壳和 DOM。
|
|
65
|
+
|
|
66
|
+
```tsx
|
|
67
|
+
function Header() {
|
|
68
|
+
const userService = useInject<UserService>(UserService);
|
|
69
|
+
const title = useValue<string>('app.public.title', 'My app');
|
|
70
|
+
return <header>{title}: {userService.name}</header>;
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## 配置(公开:前端接收)
|
|
75
|
+
|
|
76
|
+
| 键 | 默认 | |
|
|
77
|
+
| --- | --- | --- |
|
|
78
|
+
| `react.router.type` | `browser` | `hash` 用于基于哈希的 URL |
|
|
79
|
+
| `react.path` | `${server.path ?? '/'}` | 路由的基路径 |
|
|
80
|
+
| `react.router.routes` | | 按路径的路由元数据,在装饰器上合并:`{ '/about': { caseSensitive: true, priority: 2000 } }` |
|
|
81
|
+
|
|
82
|
+
前端配置在构建时评估,对加载页面的任何人可见:不要在其中放机密。
|
|
83
|
+
|
|
84
|
+
## 部件和插槽
|
|
85
|
+
|
|
86
|
+
`<Slot area="header" />` 按 `priority` 顺序渲染为一个区域注册的部件;用 `@Widget({ area: 'header', component: Logo })` 从 `@celljs/widget` 注册一个。库用这个来让应用无需改它们就能添加到它们的布局。
|
|
87
|
+
|
|
88
|
+
## 调用后端
|
|
89
|
+
|
|
90
|
+
`useRpc<T>(Id)` 返回 [`@celljs/rpc`](../../packages/rpc) 的后端服务的类型化代理;它的方法返回 promise,所以在效果或事件处理器中调用它们。对于其他 HTTP API,使用 `fetch` 或 [`@celljs/http`](../../packages/http) 的 `RestOperations`。
|
|
91
|
+
|
|
92
|
+
```tsx
|
|
93
|
+
function Greeting() {
|
|
94
|
+
const greetingServer = useRpc<GreetingServer>(GreetingServer);
|
|
95
|
+
const [text, setText] = React.useState('');
|
|
96
|
+
React.useEffect(() => { greetingServer.greet('web').then(setText); }, [greetingServer]);
|
|
97
|
+
return <p>{text}</p>;
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## 用 react-intl 翻译
|
|
102
|
+
|
|
103
|
+
react-intl 是可选的。使用时,安装它,并在 `src/browser/module.ts` 中导入 `@celljs/react/lib/browser/intl` 一次:它的 `IntlContext` 将 `IntlProvider` 与选择的区域设置的消息放在应用周围,所以 `useIntl()` 和 `<FormattedMessage>` 在每个组件中工作。
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@celljs/react",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.7.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cell-component"
|
|
@@ -33,8 +33,8 @@
|
|
|
33
33
|
"watch": "tsc -b -w --preserveWatchOutput"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@celljs/core": "4.
|
|
37
|
-
"@celljs/widget": "4.
|
|
36
|
+
"@celljs/core": "4.7.0",
|
|
37
|
+
"@celljs/widget": "4.7.0",
|
|
38
38
|
"@types/react": "^19.3.0",
|
|
39
39
|
"react": "^19.3.1",
|
|
40
40
|
"react-dom": "^19.3.1",
|