-
Notifications
You must be signed in to change notification settings - Fork 310
Fix #1151: 修正按钮渲染逻辑,避免 render 阶段副作用,改用 JSX 条件渲染 & named slot #1153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
跟 1151 没直接关系。也不是什么大问题 虽然改用 children 才是最好最合理的选择,但考虑你日后可能会增加或改变这个 FileSystemParams 的设计, named slot 跟 children 一样是 JSX 模版 供参考的AI对话 对话1你现在的 下面我按 「怎么改 + 为什么这样改 + 完整示例」 来说明。 一、设计目标把: <FileSystemParams actionButtons={[<Button ... />, ...]} />改成: <FileSystemParams>
<Button ... />
<Button ... />
<Button ... />
</FileSystemParams>这样做的好处:
二、修改
|
|
确实没注意到😅,没想到本来就是数组,AI又给它套一层,还“机智”的加上了s。 |
|
看起来改成 children 是最好,最合理的,但是我也不记得我为什么要用这种方式了 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
本 PR 将 FileSystemParams 组件从命令式的数组操作模式重构为声明式的 JSX 子元素模式,消除了 render 阶段的副作用。
Changes:
- 将
FileSystemParams的actionButton数组属性改为children插槽模式 - 将
preNode属性重命名为更语义化的headerContent - 将网盘解绑按钮的条件渲染从
push操作改为 JSX 条件渲染
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/pages/components/FileSystemParams/index.tsx | 重构组件接口,将 actionButton 改为 children,将 preNode 改为 headerContent,网盘解绑按钮改用 JSX 条件渲染 |
| src/pages/options/routes/Tools.tsx | 更新 FileSystemParams 使用方式,按钮从数组属性改为子元素 |
| src/pages/options/routes/Setting.tsx | 更新 FileSystemParams 使用方式,按钮从数组属性改为子元素 |
| src/pages/components/RuntimeSetting/index.tsx | 更新 FileSystemParams 使用方式,按钮从数组属性改为子元素 |
我沒看UI, 只看代碼改的。 |
概述 Descriptions
@CodFrm 以后AI做出来的PR 只会愈来愈多。Copliot找到的问题麻烦认真看待一下。
Codex在React元件定义里把state 做浅拷贝 再插React元件。。。太可怕了
变更内容 Changes
截图 Screenshots
为什么要做这个改动(背景)
在 React 中,render 阶段不应该产生副作用,也不应该修改已有的 state 或 props。
旧实现中,在函数组件的 render 过程中,通过
push的方式向按钮数组中追加元素,这属于 在 render 阶段修改数组引用的副作用行为。虽然目前代码仍然可以正常运行,但在以下场景中可能会产生问题:
因此,有必要对这部分代码进行重构优化。
这个 PR 做了什么改动
❌ 旧实现方式(不推荐)
在 render 阶段对数组进行操作,根据条件
push新按钮:问题在于:
✅ 新实现方式(推荐)
将按钮是否显示的逻辑直接写在 JSX 中,通过条件渲染控制:
这样做的好处是:
这次改动带来的价值
避免 render 阶段的副作用
push、mutation 等操作逻辑更清晰、语义更直观
减少潜在 Bug
更容易维护和调试
总结
这次 PR 的核心思想是:
这是一个改动不大、但代码质量明显提升的重构:
属于一次低风险、高收益的质量优化改动。