Framework/Next.js

[Next.js] Error: Invalid Link with a child. Please remove a or use Link legacyBehavior 였λ₯˜

yuri lee 2022. 11. 8. 22:30
λ°˜μ‘ν˜•

Intro

Next.jsλ₯Ό μ‚¬μš©ν•˜λ˜ 도쀑 λ‹€μŒκ³Ό 같은 μ—λŸ¬κ°€ λ°œμƒν•˜μ˜€μŠ΅λ‹ˆλ‹€. 

 

Error: Invalid <Link> with <a> child. Please remove <a> or use <Link legacyBehavior> 

 

Why?

<Link href={props.link}>
  <a className={classes.btn}>{props.children}</a>
</Link>

next/link λ₯Ό ν™œμš©ν•˜μ—¬ Link νƒœκ·Έ μ•ˆμ— a νƒœκ·Έλ₯Ό λ„£μ–΄μ€¬λ˜ μ½”λ“œμ— λ¬Έμ œκ°€ μžˆμ—ˆμŠ΅λ‹ˆλ‹€. μ—λŸ¬ 화면에 λ‚˜μ™€μžˆλŠ” Next.js μ‚¬μ΄νŠΈμ— ν•œλ²ˆ μ ‘μ†ν•΄λ΄€μŠ΅λ‹ˆλ‹€. (https://nextjs.org/docs/messages/invalid-new-link-with-extra-anchor)

 

Why This Error Occurred

Next.js 13버전이 λ‚˜μ˜€λ©΄μ„œ, Link νƒœκ·ΈλŠ”<a>둜 λ Œλ”λ§ λ˜λ―€λ‘œ <a>λ₯Ό μ‚¬μš©ν•˜λŠ” μ‹œλ„λŠ” μœ νš¨ν•˜μ§€ μ•Šλ‹€κ³  ν•˜λ„€μš”!

 

How to solve the problem

npx @next/codemod new-link .

이럴 κ²½μš°μ—λŠ” new-link codemod λ₯Ό μ‹€ν–‰ν•˜μ—¬ 이전 버전 Next.jsλ₯Ό <Link> μ‚¬μš©λ²•μ— μžλ™ μ—…κ·Έλ ˆμ΄λ“œ ν•΄μ€˜μ•Ό ν•©λ‹ˆλ‹€. λ‹€μŒ μ½”λ“œλ₯Ό μ‹€ν–‰ μ‹œ <Link><a id="link">Home<a></Link> 이 <Link id="link">Home</Link> 둜 λ³€κ²½λ©λ‹ˆλ‹€. λ˜λŠ” legacyBehavior prop을 μΆ”κ°€ν•˜μ—¬ <Link legacyBehavior><a id="link">Home<a></Link> λ‹€μŒκ³Ό 같이 μ‚¬μš©ν•  μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€. 

 

<Link href={props.link} legacyBehavior>
 <a className={classes.btn}>{props.children}</a>
</Link>

new-link λ₯Ό μ‹€ν–‰ν•œ ν›„, λ‹€μŒμœΌλ‘œ μ½”λ“œλ₯Ό λ³€κ²½ν•˜λ‹ˆ μ—λŸ¬ 없이 싀행이 λ˜μ—ˆμŠ΅λ‹ˆλ‹€.

 

ν˜„μž¬ μ €λŠ” next.js λ₯Ό κ°•μ˜(next version이 13버전 이전)λ₯Ό 보며 곡뢀λ₯Ό ν•˜κ³  μžˆλŠ”λ°μš”, λ‘œμ»¬μ—μ„œ creat next-app 을 μ‚¬μš©ν•˜μ—¬ ν”„λ‘œμ νŠΈλ₯Ό μƒμ„±ν•œ μƒνƒœλ‘œ μ§„ν–‰ν•˜λ‹€λ³΄λ‹ˆ 버전이 λ§žμ§€ μ•Šμ•„ 생긴 문제 κ°™μŠ΅λ‹ˆλ‹€. next.js κ°€ μ•„μ£Ό μΉœμ ˆν•˜κ²Œ μ—λŸ¬μ˜ 이유λ₯Ό μ•Œλ €μ€˜μ„œ κ³ λ§™λ„€μš” πŸ‘

 


https://nextjs.org/docs/messages/invalid-new-link-with-extra-anchor

λ°˜μ‘ν˜•