-
-
Notifications
You must be signed in to change notification settings - Fork 10.6k
v4 beta: How to use Link with NavItem(react-bootstrap) #4463
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
Comments
Yep, seems that |
You can do this with a render function on your Route. Just pass down the things you need from the parent. You can also use withRouter to get things off context easily. |
I'm not sure that Route with a render function does the same thing. The example from documentation: const OldSchoolMenuLink = ({ label, to, activeOnlyWhenExact }) => (
<Route path={to} exact={activeOnlyWhenExact} children={({ match }) => (
<div className={match ? 'active' : ''}>
{match ? '> ' : ''}<Link to={to}>{label}</Link>
</div>
)}/>
) The But you can just copy Link.js and add to it the ability to call children as function. The change is about 4 lines total... |
const Animations = ({match, location}) => (
<Container>
<Header as='h2'>Animations</Header>
<Grid columns={2}>
<Grid.Row>
<Grid.Column width={3}>
<Menu size='mini' vertical fluid>
<Menu.Item name='slides' as={Link} to={`${match.url}/slides`} active={location.pathname === '/animations/slides'}>
Slides
</Menu.Item>
<Menu.Item name='drawer' as={Link} to={`${match.url}/drawer`} active={location.pathname === '/animations/drawer'}>
Drawer
</Menu.Item>
<Menu.Item name='pusher' as={Link} to={`${match.url}/pusher`} active={location.pathname === '/animations/pusher'}>
Pusher
</Menu.Item>
</Menu>
</Grid.Column>
<Grid.Column width={13}>
<Route path={`${match.url}/slides`} component={Slides}/>
</Grid.Column>
</Grid.Row>
</Grid>
</Container>
);
export default Animations; In my case i just passed the location object which has a pathname key. or you can the read doc |
FYI this is how I did with material-ui : const Link = ({ to, children }) => (
<Route
children={history => React.cloneElement(children, {
href: to,
onClick: e => {
e.preventDefault();
history.push(to);
}
})}
/>
); Then I use it like this : <Link to="/users">
<MenuItem primaryText="Users" leftIcon={<ModeEditIcon />} />
</Link> |
Hi guys, Could you please explain how to handle cases when there is a custom element, only a part of which needs to play a role of clickable link? E.g. we have In v3 the solution was easy, the first comment lists it. In v4 there seems to be way to use |
Well, you probably can just inherit the Otherwise, you can use |
Inheriting a link may seem reasonable in certain cases, like when you want a menu item that works like a link. There can be many of these in the interface, so making a custom component is worth the effort. However, there can be situations when a link of a certain style is to appear only once in the whole app. For example, my 404 page contains something like this: import React from 'react';
import { Button } from 'antd'; // https://ant.design/components/button/
import { Link } from 'react-router-dom'; //v3
import styled from 'styled-components';
const LayoutForErrorPage = styled.div`
text-align: center;
`;
const EmptyPageMessage = styled.div`
font-size: 2em;
margin-top: 100px;
`;
export default () => (
<LayoutForErrorPage>
<EmptyPageMessage>Page not found</EmptyPageMessage>
<Link to="/">{
({ onClick }) =>
<Button onClick={onClick} type="primary">back to home page</Button>
}</Link>
</LayoutForErrorPage>
); The composition of link and an existing third-party button was quite straightforward in v3: no custom event handler and no new components inheriting other components. I'm not sure how to solve the same problem in v4 without introducing extra complexity. Could you please share some advice? |
Inheritance example: import React from 'react';
import { Button } from 'antd'; // https://ant.design/components/button/
import { Link } from 'react-router-dom'; // v4
import styled from 'styled-components';
const LayoutForErrorPage = styled.div`
text-align: center;
`;
const EmptyPageMessage = styled.div`
font-size: 2em;
margin-top: 100px;
`;
class LinkButton extends Link {
render() {
const {
to,
children,
...rest
} = this.props;
return (<Button {...rest} onClick={this.handleClick}>{children}</Button>);
}
}
export default () => (
<LayoutForErrorPage>
<EmptyPageMessage>Page not found</EmptyPageMessage>
<LinkButton to="/" type="primary">back to home page</LinkButton>
</LayoutForErrorPage>
); Looks like too much for such a simple use case. Feels like some generalised wrapper is needed out of box. Being able to handle target and other props that |
@vasa-chi nice tip! I've tried a Link patch that supports custom component:
|
anybody can give a code for href in navitem without using router |
Another solution, extending this answer from #83:
Works with |
With v4 alpha i can do this, and
Link
component will not generate the<a>
tag:The text was updated successfully, but these errors were encountered: