https://codesandbox.io/s/material-demo-c4f5u
class SimpleTabs extends React.Component {
constructor(props) {
super(props);
this.state = {
selectedButton: false,
value: 0
};
}
handleChange = (event, newValue) => {
this.setState({
value: newValue
});
};
selectButton = e => {
console.log("selectButton--->", e.currentTarget);
console.dir("selectButton--->", e.currentTarget);
const selectedButtonTarget = e.currentTarget;
this.setState({ selectedButton: true });
};
renderButtonSelection = () => {
if (this.state.selectedButton === true) {
return (
<div>
<div>no button selected</div>
<div>first button selected</div>
<div>second button selected</div>
<div>third button selected</div>
</div>
);
}
};
render() {
//const classes = useStyles();
const { classes } = this.props;
// const [value, setValue] = React.useState(0);
return (
<div className={classes.root}>
<AppBar position="static">
<Tabs value={this.state.value} onChange={this.handleChange}>
<Tab label="Item One" />
<Tab label="Item Two" />
<Tab label="Item Three" />
</Tabs>
</AppBar>
{this.state.value === 0 && (
<TabContainer>
Item One
<Button onClick={this.selectButton}>button one</Button>
</TabContainer>
)}
{this.state.value === 1 && (
<TabContainer>
Item Two <Button onClick={this.selectButton}>button two</Button>
</TabContainer>
)}
{this.state.value === 2 && (
<TabContainer>
Item Three <Button onClick={this.selectButton}>button three</Button>
</TabContainer>
)}
{this.renderButtonSelection()}
</div>
);
}
}
export default withStyles(styles)(SimpleTabs);
答案 0 :(得分:0)
希望这是您想要的:
用以下内容替换了selectButton和renderButtonSelection方法:
getNumStr = n => {
switch (n) {
case 1:
return "one";
case 2:
return "two";
case 3:
return "three";
default:
return "";
}
};
selectButton = e => {
const selectedButtonTarget = this.state.value + 1;
this.setState({ selectedButton: selectedButtonTarget });
};
renderButtonSelection = () => {
if (this.state.selectedButton) {
return <div>button {this.getNumStr(this.state.selectedButton)} selected</div>;
}
return null;
};