增加选项卡的数量

问题描述 投票:0回答:1

这里我试图制作一个标签栏,其中按下按钮时标签的数量增加。选项卡的内容保持不变。我需要什么要做?

react-native
1个回答
0
投票

您可以做这样的事情。在事件上增加数组的大小,然后{this.state.tabs.map((tab,idx)=>(将为您复制该组件。

import React,{Component} from 'react';
import './App.css';
import Tabs from '@material-ui/core/Tabs';
import Tab from '@material-ui/core/Tab';

class App extends Component {
  constructor(props){
    super(props)
    this.state={
      tabs:["Tab1"],
    }
  }

  replicate=()=>{
    let tabs = this.state.tabs;
    tabs.push("Tab2")
    this.setState({
      tabs
    })
  }


  render() {
    return (
      <>
       <button onClick={this.replicate}> Replicate</button>
       <Tabs
        value={"tabs"}
        indicatorColor="primary"
        textColor="primary"
        // onChange={handleChange}
        aria-label="disabled tabs example"
      >

        {/* // Here when the action is fired the array increases its size and this map replicate the tab */}

        {this.state.tabs.map((tab, idx) => (
          <Tab label="Active" />
        ))}
       </Tabs>
      </>
    );
  }
}

export default App;
© www.soinside.com 2019 - 2024. All rights reserved.