New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Menu组件应该如何结合react-router Link组件? #1575
Comments
Duplicated: #1432 Please read FAQ and search issues before open an issue, THX! It will be better if you read smart questions(提问的智慧). |
ok, 已解决~ import { browserHistory} from 'react-router';
export default class extends React.Component {
linkTo(item) {
browserHistory.push(item.key);
}
render() {
return (
<Menu mode="inline" theme="dark" defaultSelectedKeys={['page1']} onClick={this.linkTo}>
<Menu.Item key="page1">Page1</Menu.Item>
<Menu.Item key="page2">Page2</Menu.Item>
<Menu.Item key="page3">Page3</Menu.Item>
</Menu>
);
}
} |
@flicker85 这样实现不是那么完美,浏览器回退不会自动改变menu 的状态 |
export default class extends React.Component {
constructor() {
super();
this.state = {
selectedKeys: []
}
}
static contextTypes = {
router: React.PropTypes.object
}
componentWillReceiveProps() {
this.setState({ selectedKeys: [this.context.router.location.pathname] });
}
componentDidMount() {
this.setState({ selectedKeys: [this.context.router.location.pathname] });
}
linkTo(item) {
this.context.router.push(item.key);
}
render() {
return (
<Menu mode="inline" theme="dark" defaultSelectedKeys={['page1']} selectedKeys={this.state.selectedKeys} onClick={this.linkTo}>
<Menu.Item key="page1">Page1</Menu.Item>
<Menu.Item key="page2">Page2</Menu.Item>
<Menu.Item key="page3">Page3</Menu.Item>
</Menu>
);
}
} |
@flicker85 如果这个有状态的菜单放到一个无状态的父级容器组件里面,this.context undefined怎么办? |
@flicker85 我直接把你这段代码拿到项目中,this,context是undefined |
最终解决方式`import React, { PropTypes } from 'react'; const LeftMenu = React.createClass({ getInitialState() {//初始化 contextTypes :{ componentWillReceiveProps() { componentDidMount() { linkTo(item) { render() { export default LeftMenu; |
最简单的方式 |
为什么不直接这样呢 render() { |
This would be much more useful if it were in English. |
Hi I could find a solution this way using withRouter in react router It's from here : https://stackoverflow.com/questions/41054657/react-routerantd-how-to-highlight-a-menu-item-when-press-back-forward-button
feel free to add any suggestions if you think I can improve this more. |
@nadunindunil how to use SideMenu class in |
app.js will be
I used mainApp.js to make it easier. |
Look at here <MenuItem
containerElement={<Link to="/profile" />} // Here!!!
primaryText="Profile"
leftIcon={
<FontIcon className="material-icons">people</FontIcon>
}
/> |
@Cleam I think you are referring to material-ui since |
这样没有解决回退时item的高亮问题 |
awesome, can use useLocation also. |
use hashmap could resolve problem of 'Router 'history' is declared here'. |
* seperate sider to components/LeftMenu ref: ant-design/ant-design#1575
jsx
生成的代码
点击padding-left:48px的空白处会选中菜单,但是不会触发Link to的事件,有什么好的处理方式么?
The text was updated successfully, but these errors were encountered: