Skip to content

Instantly share code, notes, and snippets.

@productioncoder
Last active November 17, 2018 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save productioncoder/b7a86072a93a9a065174680979e73cd1 to your computer and use it in GitHub Desktop.
Save productioncoder/b7a86072a93a9a065174680979e73cd1 to your computer and use it in GitHub Desktop.
Youtube in React: make side bar item dynamic
/* ... */
import {Link, withRouter} from 'react-router-dom';
export class SideBarItem extends React.Component {
render() {
// React will ignore custom boolean attributes, therefore we pass a string
// we use this attribute in our SCSS for styling
const highlight = this.shouldBeHighlighted() ? 'highlight-item' : null;
return (
<Link to={{pathname: this.props.path}}>
<Menu.Item className={['sidebar-item', highlight].join(' ')}>
<div className='sidebar-item-alignment-container'>
<span><Icon size='large' name={this.props.icon}/> </span>
<span>{this.props.label}</span>
</div>
</Menu.Item>
</Link>
);
}
shouldBeHighlighted() {
const {pathname} = this.props.location;
console.log('pathname = ', pathname);
if (this.props.path === '/') {
return pathname === this.props.path;
}
return pathname.includes(this.props.path);
}
}
export default withRouter(SideBarItem);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment