question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[gatsby-plugin-react-next] <Link>'s activeClassName not respected, when pathPrefix is used

See original GitHub issue

Description

When using gatsby-plugin-react-next and pathPrefix, activeStyle or activeClassName are not taken into account when page is initial loaded. Only after manual triggering navigation, activeStyle or activeClassName are respected.

Environment

Gatsby version: 1.9.135 Node.js version: 8.7.0 Operating System: macOS 10.13.2

File contents (if changed):

gatsby-config.js:

module.exports = {
  siteMetadata: {
    title: `Gatsby Default Starter`,
  },
  plugins: [
      `gatsby-plugin-react-helmet`,
      `gatsby-plugin-react-next`,
    ],
  pathPrefix: `/my-gatsby-site`,
}

package.json:

{
  "name": "gatsby-starter-default",
  "description": "Gatsby default starter",
  "version": "1.0.0",
  "author": "Kyle Mathews <mathews.kyle@gmail.com>",
  "dependencies": {
    "gatsby": "^1.9.135",
    "gatsby-link": "^1.6.32",
    "gatsby-plugin-react-helmet": "^1.0.8",
    "gatsby-plugin-react-next": "^1.0.6"
  },
  "keywords": [
    "gatsby"
  ],
  "license": "MIT",
  "main": "n/a",
  "scripts": {
    "build": "gatsby build",
    "develop": "gatsby develop",
    "format": "prettier --trailing-comma es5 --no-semi --single-quote --write \"src/**/*.js\"",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "devDependencies": {
    "prettier": "^1.9.2"
  }
}

Actual result

Active Links should be styled bold - also on initial page load and not only after navigation did occur. Testlink: http://gatsbynext-router.surge.sh/my-gatsby-site/page-2

react16

Expected behavior

Same example as above, but without gatsby-plugin-react-next. Testlink: http://gatsbynext-router-react15.surge.sh/my-gatsby-site/page-2

react15

Steps to reproduce

  1. Setup new vanilla gatdbyJS project

  2. Add gatsby-plugin-react-next plugin

  3. Make use of activeStyle or activeClassName at the <Link> components, that come with the gatsbyJS boilerplate. Eg page-2.js:

<Link activeStyle={{ fontWeight: 'bold' }} to="/">
  Go back to the homepage
</Link>
<br />
<Link activeStyle={{ fontWeight: 'bold' }} to="/page-2/">
  Go to page 2
</Link>
  1. Add a pathPrefix (see gatsby-config.js above) and build a production version of the project using gatsby build --prefix-paths

  2. See result using eg. gatsby serve and test it by initialting page refreshes and navigation

mv public my-gatsby-site                                                                                                                                                                                    
mkdir public                                                                                                                                                                                               
mv my-gatsby-site public                                                                                                                                                                                    
gatsby serve

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
th0thcommented, Aug 15, 2018

Link’s behaviour has changed when reach/router has replaced react-router. @KyleAMathews mentioned here that new Link won’t get that capability. So I wrote my custom Link wrapper, here it is in case someone else needs:

import React from 'react';
import PropTypes from 'prop-types';
import {
  Link as GatsbyLink,
} from 'gatsby';

export default function Link(props) {
  const {
    exact,
    className,
    activeClassName,
    children,
  } = props;

  return (
    <GatsbyLink
      {...props}
      getProps={({ isCurrent, isPartiallyCurrent }) => ({
        className: [
          className,
          (exact && isCurrent) ? activeClassName : '',
          (!exact && isPartiallyCurrent) ? activeClassName : '',
        ].join(' ').trim(),
      })}
    >
      {children}
    </GatsbyLink>
  );
}

Link.propTypes = {
  exact: PropTypes.bool,
  className: PropTypes.string,
  activeClassName: PropTypes.string,
  children: PropTypes.node.isRequired,
};

Link.defaultProps = {
  exact: false,
  activeClassName: 'active',
  className: '',
};
2reactions
zebapycommented, Aug 1, 2018

Also encountering this (active links don’t get styles on page refresh until client route changes happen). @ooloth’s problem is something other than the original issue I believe.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React router Link; activeClassName not working
Instructions. Use <NavLink> instead of <Link> and add exact as a property. Include exact as a property to ensure activeClassName only ...
Read more >
NavLink - React Router: Declarative Routing for React.js
In React Router v6, activeClassName will be removed and you should use the function className to apply classnames to either active or inactive...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found