import Link from 'next/link';
export const NavigationBar = () => {
return (
<nav>
<Link href={'/'}>
<a>Home</a>
</Link>
<Link href={'/about'}>
<a>About</a>
</Link>
</nav>
);
};
The
next/link
child can no longer be<a>
. Add the legacyBehavior prop to use the legacy behavior or remove the<a>
to upgrade. A codemod is available to automatically upgrade your code.
โ Next.js๊ฐ 13๋ฒ์ ์ ๋ฆด๋ฆฌ์ฆ ํ๋ฉด์Link
์a
ํ๊ทธ๋ฅผ ์์์ผ๋ก ์ถ๊ฐํ ํ์๊ฐ ์์ด์ก๋ค!
next/link
next/link
no longer requires manually adding <a>
as a child.
This was added as an experimental option in 12.2 and is now the default. In Next.js 13, <Link>
always renders an <a>
and allows you to forward props to the underlying tag. For example:
โ Next.js 13์์ <Link>
๋ ํญ์ <a>
ํ๊ทธ๋ฅผ ๋ ๋๋งํฉ๋๋ค.
import Link from 'next/link'
// Next.js 12: `<a>` has to be nested otherwise it's excluded
<Link href="/about">
<a>About</a>
</Link>
// Next.js 13: `<Link>` always renders `<a>`
<Link href="/about">
About
</Link>
<a>
tag๋ง์ฝ a
ํ๊ทธ๋ฅผ Link
์ ์์์ผ๋ก ๋ฃ๊ณ ์ถ๋ค๋ฉด, legacyBehavior
์์ฑ์ ์ถ๊ฐํด์ค๋๋ค.
legacyBehavior
- Changes behavior so that child must be <a>
. Defaults to false
.a
ํ๊ทธ๊ฐ ์์์ผ๋ก ๋์ํ ์ ์๋๋ก ๋ณ๊ฒฝ. ๊ธฐ๋ณธ๊ฐ์ false
.import Link from 'next/link'
function Legacy() {
return (
<Link href="/about" legacyBehavior>
<a>About Us</a>
</Link>
)
}
export default Legacy
a
ํ๊ทธ๋ก ๊ฐ์ธ์ง custom component ๋๋ functional component์๋ legacyBehavior
์์ฑ์ ๋ฃ์ด์ฃผ์ด์ผ ํฉ๋๋ค.<a>
tagimport Link from 'next/link'
import styled from 'styled-components'
// This creates a custom component that wraps an <a> tag
const RedLink = styled.a`
color: red;
`
function NavLink({ href, name }) {
return (
<Link href={href} passHref legacyBehavior>
<RedLink>{name}</RedLink>
</Link>
)
}
export default NavLink
import Link from 'next/link'
// `onClick`, `href`, and `ref` need to be passed to the DOM element
// for proper handling
const MyButton = React.forwardRef(({ onClick, href }, ref) => {
return (
<a href={href} onClick={onClick} ref={ref}>
Click Me
</a>
)
})
function Home() {
return (
<Link href="/about" passHref legacyBehavior>
<MyButton />
</Link>
)
}
export default Home
์ฐธ๊ณ