React Router v4 setting activeClass on parent

前端 未结 4 1047
猫巷女王i
猫巷女王i 2021-01-17 16:13

Not too familiar with react router, but I need the functionality of the NavLink to set the active class on the parent li element, and not the a ele

4条回答
  •  时光说笑
    2021-01-17 16:52

    I am just starting with the react, so not sure if this is the best practices, but after going through router v4 docs, I used withRouter props -> location.pathname and compared it to my route.

    Here is the Navigation.js:

    import React from 'react';
    import { withRouter } from 'react-router-dom';
    import NavLink from '../General/NavLink';
    
    const activeClass = (path, link) => {
        if (path === link) {
            return true;
        }
        return false;
    };
    
    const Navigation = props => {
        const { location } = props;
        return (
            
      Dashboard Users Projects Google
    ); }; export default withRouter(Navigation);

    From there you have parent and child classes that you can use on child component.

提交回复
热议问题