1. 程式人生 > >使用react+ant design左側導航預設選中和展開

使用react+ant design左側導航預設選中和展開

1.<Menu theme='dark' mode='inline' selectedKeys={[selectedKey]} onClick={this.menuClick} onOpenChange={this.onOpenChange}

openKeys={[openKey]}</Menu>

2.this.state = {

selectedKey: this.props.location.pathname, //選中

openKey: ''" //展開}

3.點選事件

menuClick =e=>{this.setState({selectedKey:e.key})}

4.更新預設狀態

componentDidMount(){

const {pathname} = this.props.location;let path = pathname.split('/');

if(pathname.split('/').length>3){path = path[path.length-2];this.setState({openKey:path})}}

5.展開收縮更新狀態

onOpenChange = (k) => {

if(k.length>1){this.setState({openKey:k[k.length-1],});}else{this.setState({openKey:'',});}};