1

In my NextJS app's _app.tsx I am using import Router from 'next/router' in order to render different nav button styles for my top menu.

When the path is / the rendering works, however when I'm on /about the render does not work and I get the following error. Even though the boolean logic is working correctly.

Error:

Warning: Prop className did not match. Server: "nav__NavActive-sc-6btkfp-2 gJVDzS nav__NavLink-sc-6btkfp-1 bvpMWI" Client: "nav__NavLink-sc-6btkfp-1 bvpMWI"

In this screenshot below, I'm currently on the /about path and the NavActive style should be applied to the about link, but it's not.

enter image description here

The boolean console.logs on the about page:

enter image description here

However the NavActive style is still stuck on portfolio which is the '/' route.

enter image description here

_app.tsx

import React from 'react'
import Router from 'next/router'
import App, { Container } from 'next/app'
import withReduxStore from '../lib/withReduxStore'
import { Provider } from 'react-redux'

import Page from '../components/Page/Page'
import { Nav, NavLink, NavActive } from '../styles'

interface IProps {
  reduxStore: any;
  location: string;
}

const pluckRoute = (Router: any) => Router ? Router.pathname : '/';

class MoonApp extends App<IProps> {
  render() {
    const { Component, reduxStore } = this.props;

    const currentRoute = pluckRoute(Router.router);
    console.log('currentRoute', currentRoute);

    const NavPortfolio = currentRoute === '/' ? NavActive : NavLink;
    const NavAbout = currentRoute === '/about' ? NavActive : NavLink;

    console.log(currentRoute === '/');
    console.log(currentRoute === '/about');

    return (
      <Container>
        <Provider store={reduxStore}>
          <Page>
            <Nav>
              <ul>
                <li><NavPortfolio href="/">Portfolio</NavPortfolio></li>
                <li><NavAbout href="/about">About</NavAbout></li>
              </ul>
            </Nav>
            <Component />
          </Page>
        </Provider>
      </Container>
    )
  }
}

export default withReduxStore(MoonApp);

My nav styles

import styled from 'styled-components'

export const Nav = styled.div`
  width: 100px;

  ul {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    margin: 30px 0 0 30px;
  }

  li { margin-right: 1.5rem; list-style: none; }
`

export const NavLink = styled.a`
  color: ${props => props.theme.apricot};
  border: none;
  &:hover { color: ${props => props.theme.offWhite}; }
`

export const NavActive = styled(NavLink)`
  color: ${props => props.theme.offWhite};
  border-bottom: 2px solid ${props => props.theme.apricot};
`
Leon Gaban
  • 27,845
  • 80
  • 281
  • 473

3 Answers3

1

What about try this?

import Link from 'next/link'

...

const pluckRoute = (Router: any) => Router ? Router.pathname : '/';

...

const currentRoute = pluckRoute(Router.router);
const NavPortfolio = currentRoute === '/' ? NavActive : NavLink;
const NavAbout = currentRoute === '/about' ? NavActive : NavLink;

...


<li>
  <Link href={`/`}>
    <NavPortfolio>Portfolio</NavPortfolio>
  </Link>
</li>
<li>
  <Link href={`/about`}>
    <NavAbout>About</NavAbout>
  </Link>
</li>
Leon Gaban
  • 27,845
  • 80
  • 281
  • 473
kkangil
  • 966
  • 1
  • 4
  • 22
  • Thanks! Yeah the Link component correctly updates the Router.route.pathname – Leon Gaban Mar 11 '19 at 17:07
  • Oh there is something strange tho, on refresh.. even tho I'm on `/about` it still applies the style to `profile` which is path `/` and I also get the same error after refresh: `index.js:2178 Warning: Prop `className` did not match. Server: "nav__NavActive-sc-6btkfp-2 gJVDzS nav__NavLink-sc-6btkfp-1 bvpMWI" Client: "nav__NavLink-sc-6btkfp-1 bvpMWI"` – Leon Gaban Mar 11 '19 at 17:12
0

I was able to fix the issue by using component state

import React from 'react'
import Router from 'next/router'
import App, { Container } from 'next/app'
import withReduxStore from '../lib/withReduxStore'
import { Provider } from 'react-redux'

import Page from '../components/Page/Page'
import { Nav, NavLink, NavActive } from '../styles'

interface IProps {
  Component: any;
  reduxStore: any;
  pageProps: any;
  router: any;
}

interface IState {
  path: string;
}

const pluckRoute = (Router: any) => Router ? Router.pathname : '/';

const pathIs = (path: string) => {
  switch (path) {
    case '/': { return 'portfolio'; }
    case '/about': { return 'about'; }
    default: return 'portfolio';
  }
}

class MoonApp extends App<IProps, IState> {
  constructor(props: IProps) {
    super(props);

    this.state = {
      path: ''
    }
  }

  componentDidMount() {
    const path = pathIs(pluckRoute(Router.router));
    this.setState({ path });
  }

  render() {
    const { Component, reduxStore } = this.props;
    const { path } = this.state;

    const NavPortfolio = path === 'portfolio' ? NavActive : NavLink;
    const NavAbout = path === 'about' ? NavActive : NavLink;

    return (
      <Container>
        <Provider store={reduxStore}>
          <Page>
            <Nav>
              <ul>
                <li><NavPortfolio href="/">Portfolio</NavPortfolio></li>
                <li><NavAbout href="/about">About</NavAbout></li>
              </ul>
            </Nav>
            <Component />
          </Page>
        </Provider>
      </Container>
    )
  }
}

export default withReduxStore(MoonApp);
Leon Gaban
  • 27,845
  • 80
  • 281
  • 473
-1

Try this three steps, it solved for me -

  1. npm install --save-dev babel-plugin-styled-components
  2. Create a .babelrc file
  3. Paste below code into it
    {
        "plugins": [
          ["styled-components", { "ssr": true, "displayName": true, "preprocess": false } ],
        ],
        "presets": ["next/babel"]
    }

P.S - The source for this answer is GitHub

Dhruvil21_04
  • 863
  • 2
  • 10
  • 27
Nitish Jha
  • 17
  • 2
  • Everyone in that thread seems to think this is the solution ... but it doesn't solve it for me (and evidently many others) :( – machineghost Jan 31 '21 at 17:34