In the active links section of the react router tutorial, they suggest the following for using active links.
Most links in your site don't need to know they are active, usually just primary navigation links need to know. It's useful to wrap those so you don't have to remember what your activeClassName or activeStyle is everywhere.
We will use a spread operator here, the three dots. It clones our props and in this use case it clones activeClassName to our desired component for us to benefit from.
Create a new file at modules/NavLink.js that looks like this:
// modules/NavLink.js
import React from 'react'
import { Link } from 'react-router'
export default React.createClass({
render() {
return <Link {...this.props} activeClassName="active"/>
}
})
I was curious to know if there was a way to make something like the below pattern work:
// modules/Navlink.js
import React from 'react'
import { Link } from 'react-router'
export default class extends Link {
constructor(props) {
super(props);
props.activeClassName = 'active'
}
}