저는 material-ui 문서에서 Basic Tabs 예제를 사용하고 예제에서 설명하는 것을 수행하도록 수정했습니다.
원래의 기본 탭 예제에서 코드는 속성을 this.state
에 설정하여 사용중인 탭을 추적합니다. 탭 하나의 항목을 클릭 할 때 탭을 전환하려면 탭 1 안에 무언가가 클릭되면 value
속성을 업데이트하면됩니다. 나는 그것이 아래에서 일어나는 주석으로 지적했다.
import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from 'material-ui/styles';
import AppBar from 'material-ui/AppBar';
import Tabs, { Tab } from 'material-ui/Tabs';
import Typography from 'material-ui/Typography';
function TabContainer(props) {
return (
<Typography {...props} component="div" style={{ padding: 8 * 3 }}>
{props.children}
</Typography>
);
}
TabContainer.propTypes = {
children: PropTypes.node.isRequired,
};
const styles = theme => ({
root: {
flexGrow: 1,
marginTop: theme.spacing.unit * 3,
backgroundColor: theme.palette.background.paper,
},
});
class BasicTabs extends React.Component {
state = {
activeTabIndex: 0,
};
handleChange = (event, value) => {
this.setState({ activeTabIndex: value });
};
render() {
const { classes } = this.props;
const { activeTabIndex } = this.state;
return (
<div className={classes.root}>
<AppBar position="static">
<Tabs value={activeTabIndex} onChange={this.handleChange}>
<Tab label="Tab One" />
<Tab label="Tab Two" />
</Tabs>
</AppBar>
{
activeTabIndex === 0 &&
// When the user clicks on Test One or Test Two, update the state
// to display Tab 2
<div onClick={() => this.setState({ activeTabIndex: 1 })}>
<TabContainer >
Test One
</TabContainer>
<TabContainer>
Test Two
</TabContainer>
</div>
}
{
activeTabIndex === 1 &&
<div>
<TabContainer>
Test Three
</TabContainer>
<TabContainer>
Test Four
</TabContainer>
</div>
}
</div>
);
}
}
BasicTabs.propTypes = {
classes: PropTypes.object.isRequired,
};
export default withStyles(styles)(BasicTabs);
한 가지 방법은 –
@DevangNaghera가 어떻게 활성 탭을 얻을 수있는 활성 탭의 값에 따라 상태에서 활성 탭을 유지하고 때마다 탭을 클릭 상태를 변경하고 내용을 렌더링하는 것입니다? – Dhaval
''this.state.activeTabe'' 사용하기 –