首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在使用react js单击特定按钮时动态添加选项卡?

如何在使用react js单击特定按钮时动态添加选项卡?
EN

Stack Overflow用户
提问于 2021-12-02 06:57:07
回答 1查看 56关注 0票数 0

代码语言:javascript
复制
import React from 'react';
import { Tabs, Button } from 'antd';
import 'antd/dist/antd.css';

const { TabPane } = Tabs;

class App extends React.Component {
  constructor(props) {
    super(props);
    this.newTabIndex = 0;
    const panes = [
      { title: 'Tab 1', content: 'Content of Tab Pane 1', key: '1' },
      { title: 'Tab 2', content: 'Content of Tab Pane 2', key: '2' },
      { title: 'Tab 3', content: 'Content of Tab Pane 3', key: '3' },
    ];
    this.state = {
      activeKey: panes[0].key,
      panes,
    };
  }

  onChange = activeKey => {
    this.setState({ activeKey });
    console.log(activeKey);
  };

  onEdit = (targetKey, action) => {
    this[action](targetKey);
  };

  add = targetKey => {
    const {panes} = this.state;
    let { activeKey } = this.state;
    if (targetKey !== activeKey){
      panes.push({title:'Tab 3', content:'Content of tab pane 3',key: activeKey+1})
      this.setState({ panes, activeKey });
    console.log(activeKey);

    }
  };

  remove = targetKey => {
    let { activeKey } = this.state;
    let lastIndex;
    this.state.panes.forEach((pane, i) => {
      if (pane.key === targetKey) {
        lastIndex = i - 1;
      }
    });
    const panes = this.state.panes.filter(pane => pane.key !== targetKey);
    if (panes.length && activeKey === targetKey) {
      if (lastIndex >= 0) {
        activeKey = panes[lastIndex].key;
      } else {
        activeKey = panes[0].key;
      }
    }
    this.setState({ panes, activeKey });
  };

  render() {
    return (
      <div className="tab-section">
        <div style={{ marginBottom: 16 }}>
          <Button onClick={this.add}>ADD Tab-1</Button>
          <Button onClick={this.add}>ADD Tab-2</Button>
          <Button onClick={this.add}>ADD Tab-3</Button>
        </div>
        <Tabs
          hideAdd
          onChange={this.onChange}
          activeKey={this.state.activeKey}
          type="editable-card"
          onEdit={this.onEdit}
        >
          {this.state.panes.map(pane => (
            <TabPane tab={pane.title} key={pane.key}>
              {pane.content}
            </TabPane>
          ))}
        </Tabs>
      </div>
    );
  }
}

export default App;
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

下面是代码。最初,我不应该看到标签。单击该按钮,该特定选项卡应弹出。我可以每次都添加一个类似的标签。如果选项卡已经存在,那么它不应该再次弹出。

注意- "antd":"^4.16.13“"react":"^17.0.2","react-dom":"^17.0.2","react-scripts":"4.0.3”是使用的包。

这是我得到的输出,每次单击Add按钮,它都会弹出相同的选项卡-

https://i.stack.imgur.com/KhHMz.png

请帮助我在添加按钮点击时添加每个选项卡。

EN

回答 1

Stack Overflow用户

发布于 2021-12-02 07:02:03

您应该使用函数而不是对象来设置新状态。状态更新的原因是异步的,当你想要改变状态时,状态的值是不可靠的。更改add方法,如下所示:

代码语言:javascript
复制
add = targetKey => {
  let { activeKey } = this.state;
  if (targetKey !== activeKey){
    this.setState(prevState => { 
      const newState = {...prevState};
      newState.panes.push({title:'Tab 3', content:'Content of tab pane 3',key: activeKey+1});
      return newState;
    });
  }
};

还可以像这样更改remove方法:

代码语言: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/w71yLn24/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70195246

复制
相关文章

相似问题

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