Skip to content

fix: hooks violation in custom agent creation#585

Merged
yaozheng-fang merged 2 commits into
volcengine:mainfrom
FirstayZheng:fix/custom-create-blank-page
Jun 5, 2026
Merged

fix: hooks violation in custom agent creation#585
yaozheng-fang merged 2 commits into
volcengine:mainfrom
FirstayZheng:fix/custom-create-blank-page

Conversation

@FirstayZheng
Copy link
Copy Markdown
Contributor

问题描述

在"添加 Agent" -> "从 0 快速创建" -> "自定义"流程中,填写完信息后点击"生成项目"按钮,页面突然变白屏,控制台报错:
Error: Minified React error #300
Uncaught Error: Rendered fewer hooks than expected. This may be caused by an accidental early return statement.

问题定位

根因:违反了 React Hooks 规则(Hooks 必须在所有条件判断之前调用)

CustomCreate.tsx 中:

  1. 第 1046 行:if (project) 条件判断并提前返回预览模式
  2. 第 1086 行:sectionImpluseRef 声明在条件返回之后

当用户点击"生成项目"触发 setProject(proj) 后:

  • 第一次渲染:project = null,执行所有 hooks(数量 = N)
  • 第二次渲染:project !== null,在第 1046 行提前返回,跳过后续 hooks(数量 = N-1)

React 检测到 hooks 数量不一致,抛出错误导致白屏。

解决方案

  • CustomCreate.tsx:将 sectionImpl useRef 移至组件顶部(第 888 行),在所有条件返回之前声明
  • ProjectPreview.tsx:将所有 hooks 初始化移至验证逻辑之前,确保 hooks 调用顺序稳定

测试结果

✅ 开发环境验证通过:

  • 自定义 agent 创建流程正常
  • 点击"生成项目"后正确显示项目预览
  • 文件树显示 5 个生成文件(agent.py、init.py、.env.example、requirements.txt、README.md)
  • 可正常查看文件内容、下载 ZIP、返回配置

影响范围

  • 修改文件:frontend/src/create/CustomCreate.tsxfrontend/src/ui/ProjectPreview.tsx
  • 影响功能:自定义模式创建 agent
  • 风险评估:低风险,仅调整 hooks 声明顺序,不改变业务逻辑

@yaozheng-fang yaozheng-fang merged commit f58924a into volcengine:main Jun 5, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants