Skip to content

Instantly share code, notes, and snippets.

@niikkiin
Created June 25, 2020 14:51
Show Gist options
  • Save niikkiin/b74f14bcfd923936e8a88e34e041b4b1 to your computer and use it in GitHub Desktop.
Save niikkiin/b74f14bcfd923936e8a88e34e041b4b1 to your computer and use it in GitHub Desktop.
React Link on UL element
import React from 'react';
import { Container } from './sidebar.styles';
import { Route, Link } from 'react-router-dom'
export const Sidebar = () => {
return (
<Container>
<ul className='list'>
<ListItemLink activeOnlyWhenExact={true} to='/dashboard'>
Dashboard
</ListItemLink>
<ListItemLink activeOnlyWhenExact={true} to='/items'>
Items
</ListItemLink>
</ul>
</Container>
);
};
const ListItemLink = ({ to, activeOnlyWhenExact, children }) => {
return (
<Route
path={to}
exact={activeOnlyWhenExact}
children={({ match }) => (
<li className={match ? 'list-item active' : 'list-item'}>
<Link className='link-list' to={to}>{children}</Link>
</li>
)}
/>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment