독립적 스타일링 style jsx
import Link from "next/link";
import { useRouter } from "next/router";
export default function NavBar() {
const router = useRouter();
return(
<nav>
<Link href='/'>
<a className={router.pathname==='/' ? 'active' : ''}>Home</a>
</Link>
<Link href='/about'>
<a className={router.pathname==='/about' ? 'active' : ''}>About</a>
</Link>
{}
{}
{}
{}
<style jsx>
{`
nav {
background-color: tomato;
}
a {
text-decoration: none;
}
.active {
color: yellow;
}
`}
</style>
</nav>
)
}
import NavBar from "../components/NavBar";
export default function Home() {
return (
<div>
<NavBar />
{}
<h1 className="active">Hello</h1>
<style jsx>
{}
{`
a {
color: white;
}
`}
</style>
</div>
)
}
- NavBar.js에서 작성한 내용은 NavBar에서만 적용
- 다른 파일에서 같은 클래스를 사용해도 적용 안 됨(독립적인 className을 자동으로 생성하기 때문)
- 다른 파일에서 NavBar 컴포넌트에 스타일링 주려고 해도 안 됨