67일 차 회고
- NextJS style 적용
NavBar.js
${css1} ${css2}
import Link from 'next/link';
import { useRouter } from 'next/router';
import styles from './NavBar.module.css';
export default function Navbar() {
const router = useRouter();
return (
<nav>
<Link
className={`${styles.link} ${
router.pathname === '/' ? styles.active : ''
}`}
href="/"
>
Home
</Link>
<Link
className={[
styles.link,
router.pathname === '/about' ? styles.active : '',
].join(' ')}
href="/about"
>
about
</Link>
</nav>
);
}
NavBar.module.css
.link {
text-decoration: none;
}
.active {
color: tomato;
}
<nav>
<Link href="/" legacyBehavior>
<a className={router.pathname === '/' ? 'active' : ''}>Home</a>
</Link>
<Link href="/about" legacyBehavior>
<a className={router.pathname === '/about' ? 'active' : ''}>about</a>
</Link>
<style jsx>{`
nav {
background: tomato;
}
a {
text-decoration: none;
}
.active {
color: yellow;
}
`}</style>
</nav>
Link 안에 a tag를 그대로 쓰면 에러가 난다. link 에 legacyBehavior 라고 써주면 에러가 해결된다.