我有一个带有状态选项卡的组件。默认情况下,它应该打开第一个选项卡,即 state value = 0。但是,当我将该组件导入另一个组件时,它应该打开分配的状态,例如 state value = 1。请检查以下示例。我怎样才能做到这一点?
提前致谢
/**Component One***/
import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import AppBar from '@material-ui/core/AppBar';
import Tabs from '@material-ui/core/Tabs';
import Tab from '@material-ui/core/Tab';
import Typography from '@material-ui/core/Typography';
function TabContainer(props) {
return (
<Typography component="div" style={{ padding: 8 * 3 }}>
{props.children}
</Typography>
);
}
TabContainer.propTypes = {
children: PropTypes.node.isRequired,
};
const styles = theme => ({
root: {
flexGrow: 1,
backgroundColor: theme.palette.background.paper,
},
});
class SimpleTabs extends React.Component {
state = {
value: 0,
};
handleChange = (event, value) => {
this.setState({ value });
};
render() {
const { classes } = this.props;
const { value } = this.state;
return (
<div className={classes.root}>
<AppBar position="static">
<Tabs value={value} onChange={this.handleChange}>
<Tab label="Item One" />
<Tab label="Item Two" />
<Tab label="Item Three" />
</Tabs>
</AppBar>
{value === 0 && <TabContainer>Item One</TabContainer>}
{value === 1 && <TabContainer>Item Two</TabContainer>}
{value === 2 && <TabContainer>Item Three</TabContainer>}
</div>
);
}
}
SimpleTabs.propTypes = {
classes: PropTypes.object.isRequired,
};
export default withStyles(styles)(SimpleTabs);
/**Component Two**/
import React from 'react';
import SimpleTabs from './SimpleTabs'
class ComponentTwo extends Component {
render () {
return (
<SimpleTabs value={1}/>
)
}
}
export default ComponentTwo