Skip to content

Commit 2cf5e56

Browse files
authored
make components "active" using appropriate props
No need for ternaries, you can use the props "activeClassName" and "onlyActiveOnIndex" which will work accordingly. Source: https://github.com/reactjs/react-router-tutorial/tree/master/lessons/09-index-links (I know this probably won't get committed, b/c then you'd have to do a whole new video. However, if you do commit, you could comment out the old version so that people won't be completely lost when they DL the source code.)
1 parent 16fb07d commit 2cf5e56

File tree

1 file changed

+6
-6
lines changed
  • 2-react-router/src/js/components/layout

1 file changed

+6
-6
lines changed

2-react-router/src/js/components/layout/Nav.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ export default class Nav extends React.Component {
1717
render() {
1818
const { location } = this.props;
1919
const { collapsed } = this.state;
20-
const featuredClass = location.pathname === "/" ? "active" : "";
21-
const archivesClass = location.pathname.match(/^\/archives/) ? "active" : "";
22-
const settingsClass = location.pathname.match(/^\/settings/) ? "active" : "";
20+
// const featuredClass = location.pathname === "/" ? "active" : "";
21+
// const archivesClass = location.pathname.match(/^\/archives/) ? "active" : "";
22+
// const settingsClass = location.pathname.match(/^\/settings/) ? "active" : "";
2323
const navClass = collapsed ? "collapse" : "";
2424

2525
return (
@@ -35,13 +35,13 @@ export default class Nav extends React.Component {
3535
</div>
3636
<div class={"navbar-collapse " + navClass} id="bs-example-navbar-collapse-1">
3737
<ul class="nav navbar-nav">
38-
<li class={featuredClass}>
38+
<li activeClassName="active" onlyActiveOnIndex={true}>
3939
<IndexLink to="/" onClick={this.toggleCollapse.bind(this)}>Featured</IndexLink>
4040
</li>
41-
<li class={archivesClass}>
41+
<li activeClassName="active">
4242
<Link to="archives" onClick={this.toggleCollapse.bind(this)}>Archives</Link>
4343
</li>
44-
<li class={settingsClass}>
44+
<li activeClassName="active">
4545
<Link to="settings" onClick={this.toggleCollapse.bind(this)}>Settings</Link>
4646
</li>
4747
</ul>

0 commit comments

Comments
 (0)