React-单击按钮时激活下一个选项卡

React- Active next tab while clicking button

我的页面上有 4 个标签。每个选项卡包含不同的数据。

我在第一个选项卡中有按钮,主要是想在用户单击该按钮时激活下一个选项卡以显示内容。

     render(){
         return (
        <MuiThemeProvider>
            <div className="background">
                <Header/>
                <div>
                    <Card>
                        <Tabs>
                            <Tab label="General">
                                <div>
                                    <button>Normal button</button> 
     // when user clicks on this button description tab should be active
                                </div>
                            </Tab>
                            <Tab label="Descriptions">
                                <Description />
                            </Tab>

                            <Tab label="Content">
                                <Content />
                            </Tab>

                            <Tab label="Sidebar">
                                  <Sidebar/>
                            </Tab>
                        </Tabs>
                    </Card>
                </div>
            </div>
        </MuiThemeProvider>
    )
}

我该怎么做?

这是您必须做的 - 使用受控标签。分配一个状态值,确定当前打开哪个选项卡,然后单击按钮激活下一个选项卡。

//The currentTab variable holds the currently active tab
constructor(props) {
    super(props);
    this.state = {
      currentTab: 'a',
    };
 }

handleChange = (value) => {
    this.setState({
      currentTab: value,
    });
 };

render(){
         return (
        <MuiThemeProvider>
            <div className="background">
                <Header/>
                <div>
                    <Card>
                        <Tabs 
                          value={this.state.currentTab}
                          onChange={this.handleChange}
                         >
                            <Tab label="General" value="a">
                                <div>
                                    //Sets the currentTab value to "b" which corresponds to the description tab 
                                    <button onClick={()=>this.handleChange("b")}>Normal button</button> 
                                </div>
                            </Tab>
                            <Tab label="Descriptions" value="b">
                                <Description />
                            </Tab>
                            <Tab label="Content" value="c">
                                <Content />
                            </Tab>
                            <Tab label="Sidebar" value="d">
                                <Sidebar/>
                            </Tab>
                        </Tabs>
                    </Card>
                </div>
            </div>
        </MuiThemeProvider>
    )
}