首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >关闭特定选项卡时,它将关闭所有选项卡。

关闭特定选项卡时,它将关闭所有选项卡。
EN

Stack Overflow用户
提问于 2021-12-06 06:27:58
回答 1查看 71关注 0票数 2

请查看此选项卡,在关闭某个特定选项卡时,它将关闭所有打开的选项卡。我只想关闭那个特殊的标签。

代码语言:javascript
复制
remove = targetKey => {
  let { activeKey } = this.state;
  let lastIndex;
  this.setState(prevState => { 
      const newState = {...prevState};
      newState.forEach((pane, i) => {
          if (pane.key === targetKey) {
            lastIndex = i - 1;
          }
      });
      const panes = newState.filter(pane => pane.key !== targetKey);
      if (panes.length && activeKey === targetKey) {
          if (lastIndex >= 0) {
            activeKey = panes[lastIndex].key;
          } else {
            activeKey = panes[0].key;
          }
      }
      return ({panes, activeKey});
  });  
};

检查这个链接https://jsfiddle.net/yjumr4ox/1/

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-06 07:08:33

问题在于您正在迭代newState.forEachnewState.filter上的所有状态。你应该在这个州的窗格上迭代。就像这样:

代码语言:javascript
复制
remove = targetKey => {
let { activeKey } = this.state;
let lastIndex;
this.setState(prevState => { 
  const newState = {...prevState};
  newState.panes.forEach((pane, i) => {
      if (pane.key === targetKey) {
        lastIndex = i - 1;
      }
  });
  const panes = newState.panes.filter(pane => pane.key !== targetKey);
  if (panes.length && activeKey === targetKey) {
      if (lastIndex >= 0) {
        activeKey = panes[lastIndex].key;
      } else {
        activeKey = panes[0].key;
      }
  }
  return ({panes, activeKey});
});  
};
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70241587

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档