登录
注册
开源
企业版
高校版
搜索
帮助中心
使用条款
关于我们
开源
企业版
高校版
私有云
模力方舟
AI 队友
登录
注册
Gitee 2025 年度开源项目评选中
代码拉取完成,页面将自动刷新
开源项目
>
WEB应用开发
>
WebUI组件/框架
&&
捐赠
捐赠前请先登录
取消
前往登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
Watch
不关注
关注所有动态
仅关注版本发行动态
关注但不提醒动态
64
Star
724
Fork
116
GVP
openInula
/
inula
代码
Issues
24
Pull Requests
0
Wiki
统计
流水线
服务
质量分析
Jenkins for Gitee
腾讯云托管
腾讯云 Serverless
悬镜安全
阿里云 SAE
Codeblitz
SBOM
我知道了,不再自动展开
更新失败,请稍后重试!
移除标识
内容风险标识
本任务被
标识为内容中包含有代码安全 Bug 、隐私泄露等敏感信息,仓库外成员不可访问
BUG:openinula2.0复杂状态更新问题
已完成
#ICMTGJ
缺陷
fupingyezi
成员
创建于
2025-07-16 21:49
**openinula2.0框架复杂状态更新问题** openinula2.0框架下,同时更新数组和值时,会出现问题,只更新数组时,数组元素减少时会出现渲染问题。大致分为以下三种情况。 **一、参照官网:把所有状态及状态更新组织在一起**  示例代码如下 ``` import { render } from "@openinula/next"; import "./index.css"; const initialItems = [ { id: '1', name: 'Item 1' }, { id: '2', name: 'Item 2' }, { id: '3', name: 'Item 3' }, { id: '4', name: 'Item 4' }, { id: '5', name: 'Item 5' }, { id: '6', name: 'Item 6' }, ] const initialActiveKey = '1'; function App() { let customState = { items: initialItems, activeKey: initialActiveKey, setActiveKey(key) { this.activeKey = key; this.show(); }, addItem(item) { this.items = [...this.items, item]; this.setActiveKey(item.id); this.show(); }, removeItem(id) { this.items = this.items.filter(item => item.id !== id); this.setActiveKey(this.items[0]?.id || null); this.show(); }, show() { console.log(this.items, this.activeKey); } }; const handleChangeItem = (id, action) => { if (action === 'add') { customState.addItem({ id, name: `Item ${id}` }); } else if (action === 'remove') { customState.removeItem(id); } } const hanleChangeActiveKey = (key) => { customState.setActiveKey(key); } return ( <div style={{ display: "flex", flexDirection: 'column', gap: 20 }}> <for each={customState.items}> {(item) => ( <div key={item.id}> <text className={`${customState.activeKey ===item.id ? "active": ""}`} onClick={() => hanleChangeActiveKey(item.id)}>{item.name}</text> <button onClick={() => handleChangeItem(item.id, 'remove')}>Remove</button> </div> )} </for> <button onClick={() => handleChangeItem(Date.now().toString(), 'add')}>Add</button> </div> ) } render(<App />, document.getElementById("root")); ``` 具体情况:以上代码打印均无问题,即customState状态的更新没有问题,但是activeKey改变、items增加减少都不会触发页面渲染。 **二、状态组织在一起,更新函数单独列出** ``` import { render } from "@openinula/next"; import "./index.css"; const initialItems = [ { id: '1', name: 'Item 1' }, { id: '2', name: 'Item 2' }, { id: '3', name: 'Item 3' }, { id: '4', name: 'Item 4' }, { id: '5', name: 'Item 5' }, { id: '6', name: 'Item 6' }, ] const initialActiveKey = '1'; function App() { let customState = { items: initialItems, activeKey: initialActiveKey, } const handleChangeItem = (id, action) => { if (action === 'add') { // customState = { // ...customState, // items: [...customState.items, { id, name: `Item ${id}` }], // activeKey: id, // } customState.items = [...customState.items, { id, name: `Item ${id}` }]; customState.activeKey = id; } else if (action === 'remove') { // customState = { // ...customState, // items: customState.items.filter(item => item.id!== id), // activeKey: customState.items[0]?.id || null, // } customState.items = customState.items.filter(item => item.id !== id); customState.activeKey = customState.items[0]?.id || null; } } const hanleChangeActiveKey = (key) => { // customState = { // ...customState, // activeKey: key, // } customState.activeKey = key; } let customItems = initialItems; let customActiveKey = initialActiveKey; const handleChangeItem = (id, action) => { if (action === 'add') { customItems = [...customItems, { id, name: `Item ${id}` }]; customActiveKey = id; } else if (action ==='remove') { customItems = customItems.filter(item => item.id !== id); customActiveKey = customItems[0]?.id || null; } } const hanleChangeActiveKey = (key) => { customActiveKey = key; } return ( <div style={{ display: "flex", flexDirection: 'column', gap: 20 }}> <for each={customState.items}> {(item) => ( <div key={item.id}> <text className={`${customState.activeKey ===item.id ? "active": ""}`} onClick={() => hanleChangeActiveKey(item.id)}>{item.name}</text> <button onClick={() => handleChangeItem(item.id, 'remove')}>Remove</button> </div> )} </for> <button onClick={() => handleChangeItem(Date.now().toString(), 'add')}>Add</button> </div> ) } render(<App />, document.getElementById("root")); ``` 具体情况:此时纯activeKey改变没有问题。 添加item不改变activeKey时(即注释掉customState.activeKey = id;)没有问题,但如果同时改变activeKey,旧activeKey不会消失,即如果以初始状态为起点点击add,界面上会更新新的item,但是第一个item和新加的每一个item都会被渲染成active状态,此时点击任何一个item所有item都会变回非active状态。 删除item时,除新加的立即点击删除和第一个item可以直接删除之外,其他item点击删除都需要点两次,打印发现其他第一次点击删除时打印的activeKey都是undefined,另外删除item的bug与activeKey是否同时改变暂时没发现联系。 **三、状态分开写** ``` import { didMount, render } from "@openinula/next"; import "./index.css"; const initialItems = [ { id: '1', name: 'Item 1' }, { id: '2', name: 'Item 2' }, { id: '3', name: 'Item 3' }, { id: '4', name: 'Item 4' }, { id: '5', name: 'Item 5' }, { id: '6', name: 'Item 6' }, ] const initialActiveKey = '1'; function App() { let customItems = initialItems; let customActiveKey = initialActiveKey; const handleChangeItem = (id, action) => { if (action === 'add') { customItems = [...customItems, { id, name: `Item ${id}` }]; customActiveKey = id; } else if (action ==='remove') { customItems = customItems.filter(item => item.id !== id); customActiveKey = customItems[0]?.id || null; } } const hanleChangeActiveKey = (key) => { customActiveKey = key; } return ( <div style={{ display: "flex", flexDirection: 'column', gap: 20 }}> <for each={customItems}> {(item) => ( <div key={item.id}> <text className={`${customActiveKey ===item.id ? "active": ""}`} onClick={() => hanleChangeActiveKey(item.id)}>{item.name}</text> <button onClick={() => handleChangeItem(item.id, 'remove')}>Remove</button> </div> )} </for> <button onClick={() => handleChangeItem(Date.now().toString(), 'add')}>Add</button> </div> ) } render(<App />, document.getElementById("root")); ``` 具体情况:此时纯activeKey改变没有问题, 删除和增加item时activeKey同时改变只会改变active状态,渲染的item个数不会改变。 如果删除和增加item的同时不改变active状态,增加没问题,删除与第二种情况一样,只有新加的item和第一个item能够直接删除,其他删除都需要点两次。
**openinula2.0框架复杂状态更新问题** openinula2.0框架下,同时更新数组和值时,会出现问题,只更新数组时,数组元素减少时会出现渲染问题。大致分为以下三种情况。 **一、参照官网:把所有状态及状态更新组织在一起**  示例代码如下 ``` import { render } from "@openinula/next"; import "./index.css"; const initialItems = [ { id: '1', name: 'Item 1' }, { id: '2', name: 'Item 2' }, { id: '3', name: 'Item 3' }, { id: '4', name: 'Item 4' }, { id: '5', name: 'Item 5' }, { id: '6', name: 'Item 6' }, ] const initialActiveKey = '1'; function App() { let customState = { items: initialItems, activeKey: initialActiveKey, setActiveKey(key) { this.activeKey = key; this.show(); }, addItem(item) { this.items = [...this.items, item]; this.setActiveKey(item.id); this.show(); }, removeItem(id) { this.items = this.items.filter(item => item.id !== id); this.setActiveKey(this.items[0]?.id || null); this.show(); }, show() { console.log(this.items, this.activeKey); } }; const handleChangeItem = (id, action) => { if (action === 'add') { customState.addItem({ id, name: `Item ${id}` }); } else if (action === 'remove') { customState.removeItem(id); } } const hanleChangeActiveKey = (key) => { customState.setActiveKey(key); } return ( <div style={{ display: "flex", flexDirection: 'column', gap: 20 }}> <for each={customState.items}> {(item) => ( <div key={item.id}> <text className={`${customState.activeKey ===item.id ? "active": ""}`} onClick={() => hanleChangeActiveKey(item.id)}>{item.name}</text> <button onClick={() => handleChangeItem(item.id, 'remove')}>Remove</button> </div> )} </for> <button onClick={() => handleChangeItem(Date.now().toString(), 'add')}>Add</button> </div> ) } render(<App />, document.getElementById("root")); ``` 具体情况:以上代码打印均无问题,即customState状态的更新没有问题,但是activeKey改变、items增加减少都不会触发页面渲染。 **二、状态组织在一起,更新函数单独列出** ``` import { render } from "@openinula/next"; import "./index.css"; const initialItems = [ { id: '1', name: 'Item 1' }, { id: '2', name: 'Item 2' }, { id: '3', name: 'Item 3' }, { id: '4', name: 'Item 4' }, { id: '5', name: 'Item 5' }, { id: '6', name: 'Item 6' }, ] const initialActiveKey = '1'; function App() { let customState = { items: initialItems, activeKey: initialActiveKey, } const handleChangeItem = (id, action) => { if (action === 'add') { // customState = { // ...customState, // items: [...customState.items, { id, name: `Item ${id}` }], // activeKey: id, // } customState.items = [...customState.items, { id, name: `Item ${id}` }]; customState.activeKey = id; } else if (action === 'remove') { // customState = { // ...customState, // items: customState.items.filter(item => item.id!== id), // activeKey: customState.items[0]?.id || null, // } customState.items = customState.items.filter(item => item.id !== id); customState.activeKey = customState.items[0]?.id || null; } } const hanleChangeActiveKey = (key) => { // customState = { // ...customState, // activeKey: key, // } customState.activeKey = key; } let customItems = initialItems; let customActiveKey = initialActiveKey; const handleChangeItem = (id, action) => { if (action === 'add') { customItems = [...customItems, { id, name: `Item ${id}` }]; customActiveKey = id; } else if (action ==='remove') { customItems = customItems.filter(item => item.id !== id); customActiveKey = customItems[0]?.id || null; } } const hanleChangeActiveKey = (key) => { customActiveKey = key; } return ( <div style={{ display: "flex", flexDirection: 'column', gap: 20 }}> <for each={customState.items}> {(item) => ( <div key={item.id}> <text className={`${customState.activeKey ===item.id ? "active": ""}`} onClick={() => hanleChangeActiveKey(item.id)}>{item.name}</text> <button onClick={() => handleChangeItem(item.id, 'remove')}>Remove</button> </div> )} </for> <button onClick={() => handleChangeItem(Date.now().toString(), 'add')}>Add</button> </div> ) } render(<App />, document.getElementById("root")); ``` 具体情况:此时纯activeKey改变没有问题。 添加item不改变activeKey时(即注释掉customState.activeKey = id;)没有问题,但如果同时改变activeKey,旧activeKey不会消失,即如果以初始状态为起点点击add,界面上会更新新的item,但是第一个item和新加的每一个item都会被渲染成active状态,此时点击任何一个item所有item都会变回非active状态。 删除item时,除新加的立即点击删除和第一个item可以直接删除之外,其他item点击删除都需要点两次,打印发现其他第一次点击删除时打印的activeKey都是undefined,另外删除item的bug与activeKey是否同时改变暂时没发现联系。 **三、状态分开写** ``` import { didMount, render } from "@openinula/next"; import "./index.css"; const initialItems = [ { id: '1', name: 'Item 1' }, { id: '2', name: 'Item 2' }, { id: '3', name: 'Item 3' }, { id: '4', name: 'Item 4' }, { id: '5', name: 'Item 5' }, { id: '6', name: 'Item 6' }, ] const initialActiveKey = '1'; function App() { let customItems = initialItems; let customActiveKey = initialActiveKey; const handleChangeItem = (id, action) => { if (action === 'add') { customItems = [...customItems, { id, name: `Item ${id}` }]; customActiveKey = id; } else if (action ==='remove') { customItems = customItems.filter(item => item.id !== id); customActiveKey = customItems[0]?.id || null; } } const hanleChangeActiveKey = (key) => { customActiveKey = key; } return ( <div style={{ display: "flex", flexDirection: 'column', gap: 20 }}> <for each={customItems}> {(item) => ( <div key={item.id}> <text className={`${customActiveKey ===item.id ? "active": ""}`} onClick={() => hanleChangeActiveKey(item.id)}>{item.name}</text> <button onClick={() => handleChangeItem(item.id, 'remove')}>Remove</button> </div> )} </for> <button onClick={() => handleChangeItem(Date.now().toString(), 'add')}>Add</button> </div> ) } render(<App />, document.getElementById("root")); ``` 具体情况:此时纯activeKey改变没有问题, 删除和增加item时activeKey同时改变只会改变active状态,渲染的item个数不会改变。 如果删除和增加item的同时不改变active状态,增加没问题,删除与第二种情况一样,只有新加的item和第一个item能够直接删除,其他删除都需要点两次。
评论 (
1
)
登录
后才可以发表评论
状态
已完成
已完成
待确认
已确认
修复中
已修复
已验收
已拒绝
负责人
未设置
标签
未设置
项目
未立项任务
未立项任务
里程碑
未关联里程碑
未关联里程碑
Pull Requests
未关联
未关联
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
未关联
分支 (20)
标签 (10)
master
dev-tools
inula2-rust-compiler
kyzx-2025
openinula-migrator
vue-adapter
v2h-elselect-fix
v2h
修复代码分支
feat-website-next
dev
redux-compatibility-test
useSyncExternalStore
changeset-release/master
coderabbitai/utg/f8e6f08
bugfix-type
bugfix-dep
fix/release-new
reconciler
reactive
inula-request@1.1.9
inula-request@1.1.8
openinula@1.0.0
openinula@0.1.14
create-inula@1.0.22
inula-cli@1.0.45
inula-intl@1.0.34
inula-request@1.1.4
inula-router@1.0.16
openinula@0.1.13
开始日期   -   截止日期
-
置顶选项
不置顶
置顶等级:高
置顶等级:中
置顶等级:低
优先级
不指定
严重
主要
次要
不重要
预计工期
(小时)
参与者(1)
TypeScript
1
https://gitee.com/openInula/inula.git
git@gitee.com:openInula/inula.git
openInula
inula
inula
点此查找更多帮助
搜索帮助
Git 命令在线学习
如何在 Gitee 导入 GitHub 仓库
Git 仓库基础操作
企业版和社区版功能对比
SSH 公钥设置
如何处理代码冲突
仓库体积过大,如何减小?
如何找回被删除的仓库数据
Gitee 产品配额说明
GitHub仓库快速导入Gitee及同步更新
什么是 Release(发行版)
将 PHP 项目自动发布到 packagist.org
评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册