javascript - Ứng dụng NextJS React với styled component không hiển thị đúng: Cảnh báo: Prop `className` không khớp
Trong ứng dụng NextJS của tôi, trong _app.tsx, tôi đang sử dụng import Router from 'next/router' để hiển thị các kiểu nút điều hướng khác nhau cho menu trên cùng của tôi.
Khi đường dẫn là / việc hiển thị hoạt động, tuy nhiên khi tôi ở /about việc hiển thị không hoạt động và tôi nhận được lỗi sau. Mặc dù logic boolean đang hoạt động chính xác.
Lỗi :
Cảnh báo: Prop
classNamekhông khớp. Máy chủ: "nav__NavActive-sc-6btkfp-2 gJVDzS nav__NavLink-sc-6btkfp-1 bvpMWI" Khách: "nav__NavLink-sc-6btkfp-1 bvpMWI"
Trong ảnh chụp màn hình bên dưới, tôi hiện đang ở đường dẫn /about và kiểu NavActive nên được áp dụng cho liên kết about, nhưng lại không.
Các lệnh console.log kiểu boolean trên trang giới thiệu:
Tuy nhiên, kiểu NavActive vẫn bị kẹt ở portfolio vốn là tuyến '/'.
_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);
Các kiểu điều hướng của tôi
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};
`
Câu trả lời #1
Thử cái này thì sao?
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>
Câu trả lời #2
Tôi đã có thể sửa lỗi bằng cách sử dụng trạng thái của thành phần
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);


