|
| 1 | +# WebCell 从 v2 到 v3 的迁移 |
| 2 | + |
| 3 | +## 类 React 状态管理已被完全移除 |
| 4 | + |
| 5 | +**WebCell v3** 受到了 [**MobX** 的 **本地可观察状态** 思想][1] 的深刻启发,[不仅仅是 React][2],Web Components 可以更轻松地管理 **内部状态和逻辑**,无需任何复杂的操作: |
| 6 | + |
| 7 | +1. 状态类型声明 |
| 8 | +2. `this.state` 声明及其类型注解/断言 |
| 9 | +3. `this.setState()` 方法的调用及其回调 |
| 10 | +4. 令人困惑的 _Hooks API_... |
| 11 | + |
| 12 | +只需像管理 **全局状态** 一样声明一个 **状态存储类**,并在 `this`(即 **Web Component 实例**)上初始化它。然后像 [MobX][3] 一样使用并观察这些状态,一切就完成了。 |
| 13 | + |
| 14 | +```diff |
| 15 | +import { |
| 16 | + component, |
| 17 | ++ observer, |
| 18 | +- mixin, |
| 19 | +- createCell, |
| 20 | +- Fragment |
| 21 | +} from 'web-cell'; |
| 22 | ++import { observable } from 'mobx'; |
| 23 | + |
| 24 | +-interface State { |
| 25 | ++class State { |
| 26 | ++ @observable |
| 27 | +- key: string; |
| 28 | ++ accessor key = ''; |
| 29 | +} |
| 30 | + |
| 31 | +@component({ |
| 32 | + tagName: 'my-tag' |
| 33 | +}) |
| 34 | ++@observer |
| 35 | +-export class MyTag extends mixin<{}, State>() { |
| 36 | ++export class MyTag extends HTMLElement { |
| 37 | +- state: Readonly<State> = { |
| 38 | +- key: 'value' |
| 39 | +- }; |
| 40 | ++ state = new State(); |
| 41 | + |
| 42 | +- render({}: any, { key }: State) { |
| 43 | ++ render() { |
| 44 | ++ const { key } = this.state; |
| 45 | + |
| 46 | + return <>{value}</>; |
| 47 | + } |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +同时,`shouldUpdate() {}` 生命周期方法已被移除。你只需在 `State` 类的方法中,在状态改变之前控制逻辑即可。 |
| 52 | + |
| 53 | +## DOM 属性变为可观察数据 |
| 54 | + |
| 55 | +**DOM 属性** 不同于 React 的 props,它们是 **响应式的**。它们不仅负责 **更新组件视图**,还会与 **HTML 属性同步**。 |
| 56 | + |
| 57 | +MobX 的 [`@observable`][4] 和 [`reaction()`][5] 是实现上述功能的优秀 API,代码也非常清晰,因此我们添加了 `mobx` 包作为依赖: |
| 58 | + |
| 59 | +```shell |
| 60 | +npm install mobx |
| 61 | +``` |
| 62 | + |
| 63 | +另一方面,[`mobx-web-cell` 适配器][6] 已经合并到了核心包中。 |
| 64 | + |
| 65 | +```diff |
| 66 | ++import { JsxProps } from 'dom-renderer'; |
| 67 | +import { |
| 68 | +- WebCellProps, |
| 69 | + component, |
| 70 | + attribute, |
| 71 | +- watch, |
| 72 | ++ observer, |
| 73 | +- mixin, |
| 74 | +- createCell, |
| 75 | +- Fragment |
| 76 | +} from 'web-cell'; |
| 77 | +-import { observer } from 'mobx-web-cell'; |
| 78 | ++import { observable } from 'mobx'; |
| 79 | + |
| 80 | +-export interface MyTagProps extends WebCellProps { |
| 81 | ++export interface MyTagProps extends JsxProps<HTMLElement> { |
| 82 | + count?: number |
| 83 | +} |
| 84 | + |
| 85 | +@component({ |
| 86 | + tagName: 'my-tag' |
| 87 | +}) |
| 88 | +@observer |
| 89 | +-export class MyTag extends mixin<MyTagProps>() { |
| 90 | ++export class MyTag extends HTMLElement { |
| 91 | ++ declare props: MyTagProps; |
| 92 | + |
| 93 | + @attribute |
| 94 | +- @watch |
| 95 | ++ @observable |
| 96 | +- count = 0; |
| 97 | ++ accessor count = 0; |
| 98 | + |
| 99 | +- render({ count }: MyTagProps) { |
| 100 | ++ render() { |
| 101 | ++ const { count } = this; |
| 102 | + |
| 103 | + return <>{count}</>; |
| 104 | + } |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +## 使用 Shadow DOM 的 `mode` 选项控制渲染目标 |
| 109 | + |
| 110 | +### 渲染到 `children` |
| 111 | + |
| 112 | +```diff |
| 113 | +import { |
| 114 | + component, |
| 115 | +- mixin |
| 116 | +} from 'web-cell'; |
| 117 | + |
| 118 | +@component({ |
| 119 | + tagName: 'my-tag', |
| 120 | +- renderTarget: 'children' |
| 121 | +}) |
| 122 | +-export class MyTag extends mixin() { |
| 123 | ++export class MyTag extends HTMLElement { |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +### 渲染到 `shadowRoot` |
| 128 | + |
| 129 | +```diff |
| 130 | +import { |
| 131 | + component, |
| 132 | +- mixin |
| 133 | +} from 'web-cell'; |
| 134 | + |
| 135 | +@component({ |
| 136 | + tagName: 'my-tag', |
| 137 | +- renderTarget: 'shadowRoot' |
| 138 | ++ mode: 'open' |
| 139 | +}) |
| 140 | +-export class MyTag extends mixin() { |
| 141 | ++export class MyTag extends HTMLElement { |
| 142 | +} |
| 143 | +``` |
| 144 | + |
| 145 | +## 将 Shadow CSS 注入移动到 `render()` |
| 146 | + |
| 147 | +这样使得 **Shadow CSS** 可以随着可观察数据的更新而响应。 |
| 148 | + |
| 149 | +```diff |
| 150 | ++import { stringifyCSS } from 'web-utility'; |
| 151 | +import { |
| 152 | + component, |
| 153 | +- mixin |
| 154 | +} from 'web-cell'; |
| 155 | + |
| 156 | +@component({ |
| 157 | + tagName: 'my-tag', |
| 158 | +- renderTarget: 'shadowRoot', |
| 159 | ++ mode: 'open', |
| 160 | +- style: { |
| 161 | +- ':host(.active)': { |
| 162 | +- color: 'red' |
| 163 | +- } |
| 164 | +- } |
| 165 | +}) |
| 166 | +-export class MyTag extends mixin() { |
| 167 | ++export class MyTag extends HTMLElement { |
| 168 | + render() { |
| 169 | + return <> |
| 170 | ++ <style> |
| 171 | ++ {stringifyCSS({ |
| 172 | ++ ':host(.active)': { |
| 173 | ++ color: 'red' |
| 174 | ++ } |
| 175 | ++ })} |
| 176 | ++ </style> |
| 177 | + test |
| 178 | + </>; |
| 179 | + } |
| 180 | +} |
| 181 | +``` |
| 182 | + |
| 183 | +## 替换部分 API |
| 184 | + |
| 185 | +1. `mixin()` => `HTMLElement` 及其子类 |
| 186 | +2. `mixinForm()` => `HTMLElement` 和 `@formField` |
| 187 | +3. `@watch` => `@observable accessor` |
| 188 | + |
| 189 | +## 附录:v3 原型 |
| 190 | + |
| 191 | +1. [旧架构](https://codesandbox.io/s/web-components-jsx-i7u60?file=/index.tsx) |
| 192 | +2. [现代架构](https://codesandbox.io/s/mobx-web-components-pvn9rf?file=/src/WebComponent.ts) |
| 193 | +3. [MobX 精简版](https://codesandbox.io/s/mobx-lite-791eg?file=/src/index.ts) |
| 194 | + |
| 195 | +[1]: https://github.com/mobxjs/mobx/blob/mobx4and5/docs/refguide/observer-component.md#local-observable-state-in-class-based-components |
| 196 | +[2]: https://fcc-cd.dev/article/translation/3-reasons-why-i-stopped-using-react-setstate/ |
| 197 | +[3]: https://github.com/mobxjs/mobx/tree/mobx4and5/docs |
| 198 | +[4]: https://github.com/mobxjs/mobx/blob/mobx4and5/docs/refguide/observable-decorator.md |
| 199 | +[5]: https://github.com/mobxjs/mobx/blob/mobx4and5/docs/refguide/reaction.md |
| 200 | +[6]: https://github.com/EasyWebApp/WebCell/tree/v2/MobX |
0 commit comments