目标:知道 props、state、render、effect、class lifecycle 的关系,能解释“为什么状态变了 UI 会变”。
无论是 class component 还是 function component,都可以先按这个模型读:
props + state -> render() -> JSX -> React 更新页面
| 概念 | 谁拥有 | AIHom 读法 |
|---|---|---|
| props | 父组件或 connect 注入 | 这个组件从外面收到什么 |
| state | 组件自己或 manager | 这个组件/业务当前记住了什么 |
| render | React 调用 | 根据数据画出什么 UI |
// class 写法 componentDidMount() { start(); } componentWillUnmount() { stop(); } // hooks 写法 useEffect(() => { start(); return () => stop(); }, []);
读法:组件出现时做初始化,组件销毁时清理订阅、计时器、消息监听。
ai-home 有自己的状态管理:ConversationManager、ViewerStateManager、RootStateManager、@qunhe/reactive-framework。小组件临时状态用 useState;跨组件业务状态优先找 manager。
@observer 或 connect。React 17 项目可先读旧版 React 文档的 Components and Props、State and Lifecycle。