A
A
Archakov Dennis2017-07-06 11:57:52
React
Archakov Dennis, 2017-07-06 11:57:52

React Router 4: How to put actleClassName for two links?

Tell me how to make it so that activeClassNameone link is placed, depending on the route?
If you go to the main section - /or to /accounts, a class was set for one <NavLink>
There is a section with accounts. If you go to the main route, a section of accounts opens as if you go to the section/accounts

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vaskadogana, 2017-07-06
@archakov06

I do like this,

import React from "react";
import { Link }  from 'react-router-dom'

export default class NavLink extends React.Component{
  constructor(props) {
  	super(props);

  }

  render() {
  	let activeStyle;
  	let url = window.location.hash;
  	url = url.replace('#/','/')
  	if (url == this.props.to){
  		activeStyle = { opacity: 0.5 }
  	}
    return <Link {...this.props} style={activeStyle} />
  }
};

'react-router' has a default property: activeStyle

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question