κΈ°λ₯ κ΅¬μ± μμλ₯Ό <Link/>
μ μμμΌλ‘ μ¬μ©νλ©΄ λ€μκ³Ό κ°μ μμΈμ΄λ©λλ€.
Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?
μ΄κ²μ v8 => v9μμ μ κ·Έλ μ΄λ ν νμ λ§ ββλ°μνμ΅λλ€.
볡μ μ μ₯μ : https://github.com/nickluger/nextjs-link-ref-functional-comp-warning
<Link/>
λ κΈ°λ₯ κ΅¬μ± μμμ ν¨κ» μλν΄μΌνλ©° κ²½κ³ λ₯Ό νμνμ§ μμ΅λλ€.
μ¬κΈ°λ λ§μ°¬κ°μ§μ λλ€. λ€μκ³Ό κ°μ μ¬μ©μ μ§μ κ΅¬μ± μμλ₯Ό μ¬μ©νλ©΄ μ€λ₯κ° λ°μν©λλ€.
<Link href="/href">
<Button
type="custom"
color="#FCFCFC"
>
buttonText
</Button>
</Link>
μ΄λ κ΅¬μ± μμλ₯Ό μμμΌλ‘ μ¬μ©ν λλ§ λ°μν©λλ€. μλ₯Ό λ€μ΄ κ²½κ³ λ₯Ό μ 곡νμ§ μμ΅λλ€.
<Link href="/href">
<button
type="custom"
color="#FCFCFC"
>
buttonText
</button>
</Link>
<Link href="/href">
<a>
<Button
type="custom"
color="#FCFCFC"
>
buttonText
</Button>
</a>
</Link>
μ΄ λ²κ·Έλ₯Ό ν΄κ²°ν λ°©λ²μ΄ μμ΅λκΉ? μ°λ¦¬λ νμ΄ν μ€ν¬λ¦½νΈ μ§μκ³Ό κ°μ μΌλΆ κΈ°λ₯μ μ€μ λ‘ νμ©νκ³ μΆμ κ²½μ° 9λ‘ μ΄λνλ κ²μ΄ μ°¨λ¨λμμ΅λλ€. 9.0.3μΌλ‘ μΆ©λ ν κ²μ μμ μ°¨ λ Έμ΅λλ€. : /
λ€μκ³Ό κ°μ΄ μΌμμ μΌλ‘ μμκ±°λ₯Ό μλ ν μ μμ΅λλ€.
// TODO: Muting error, fix as soon as zeit/next.js/issues/7915 resolved
const originalError = console.error;
console.error = (...args) => {
if (/Warning.*Function components cannot be given refs/.test(args[0])) {
return;
}
originalError.call(console, ...args);
};
μ¬κΈ°μλ κ°μ λ¬Έμ κ° μμ΅λλ€.
<Link/>
λ₯Ό μ¬μ©νμ¬ μ¬μ©μ μ§μ κ΅¬μ± μμλ₯Ό λννλ κ²½μ° λ€μκ³Ό κ°μ΄ ref
μ λ¬ν μ μμ΅λλ€.
const CustomComponent = React.forwardRef(function CustomComponent(props, ref) {
return (
<div/>
);
});
<Link href={"/"}>
<CustomComponent/>
</Link>
κ²½κ³ κ° μ¬λΌμ§λλ€.
9.0.4-canary.2
μ
λ°μ΄νΈνμ§λ§ μ¬μ ν 본체μ κ²½κ³ κ° νμλ©λλ€. λ΄κ° λκ°λ₯Ό λμΉκ³ μμ΅λκΉ?
Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?
Check the render method of `Link`.
@ th0th κ΅¬μ± μμλ₯Ό React.forwardRef
λνν΄μΌν©λλ€. μ΄ κ°μ:
import React from 'react'
import Link from 'next/link'
const CustomComponent = React.forwardRef((props, ref) => (
<a ref={ref} {...props}>
Click
</a>
))
export default () => (
<Link href='/' passHref>
<CustomComponent/>
</Link>
)
Link
λ΄μμ μ¬μ©λλ λͺ¨λ κΈ°λ₯ κ΅¬μ± μμλ₯Ό μλμΌλ‘ λν νμκ² μ΅λκΉ? μ΄λ₯Όμν λ μ½κ³ λ λ°λ³΅μ μΈ μ루μ
μ΄ μμ΄μΌνμ§ μμ΅λκΉ? μ μ΄λ <Link href="/" forwardRef>...</Link>
?
@nicklugerμ λμνλ©΄ λ° μ§κ΄μ μΌλ‘ 보μ λλ€.
λ€λ₯Έ λͺ¨λ λΌμ΄λΈλ¬λ¦¬κ° λμΌν λ°©μμΌλ‘ forwardRef
μμ±μ ꡬνν΄μΌν©λκΉ? μλλ©΄ 리 μ‘νΈ ν¬λ¦¬μμ΄ν°λ κ°λ°μκ° forwardRef
μμ±μ ꡬννλ λμ React.forwardRef
ν¨μλ₯Ό μ¬μ©ν κ²μ΄λΌκ³ μκ°νμκΉμ?
μ @nicklugerμ λμν©λλ€. Link
λ΄λΆμ λͺ¨λ κ΅¬μ± μμμ λν΄ μλμΌλ‘μ΄ μμ
μ μννλ κ²μ κ³ ν΅μ
λλ€.
μ΄ λ¬Έμ λ₯Ό λ€μ μ΄κ³ (@ijjk) μ΄λ¦μ λ³κ²½ν΄μΌν©λκΉ? μλλ©΄ λκ΅°κ° μ΄λ―Έ μ λ¬Έμ λ₯Ό μ΄μμ΅λκΉ?
μ¬κΈ°λ λ§μ°¬κ°μ§μ λλ€. λ€μκ³Ό κ°μ μ¬μ©μ μ§μ κ΅¬μ± μμλ₯Ό μ¬μ©νλ©΄ μ€λ₯κ° λ°μν©λλ€.
<Link href="/href"> <Button type="custom" color="#FCFCFC" > buttonText </Button> </Link>
μ΄λ κ΅¬μ± μμλ₯Ό μμμΌλ‘ μ¬μ©ν λλ§ λ°μν©λλ€. μλ₯Ό λ€μ΄ κ²½κ³ λ₯Ό μ 곡νμ§ μμ΅λλ€.
<Link href="/href"> <button type="custom" color="#FCFCFC" > buttonText </button> </Link> <Link href="/href"> <a> <Button type="custom" color="#FCFCFC" > buttonText </Button> </a> </Link>
μ½μ μ€λ₯κ° λ°μν κ°λ₯μ±μ΄ λμ΅λλ€.
Warning: validateDOMNesting(...): <a> cannot appear as a descendant of <a>.
Next JSλ₯Ό μ κ·Έλ μ΄λ ν λμ΄ λ¬Έμ κ° λ°μνμ΅λλ€. κ°λ κΈΈμ μμ μ΄ μμ΅λκΉ?
forwardRefλ₯Ό μ¬μ©νκ³ κ΅¬μ± μμλ₯Ό λννλ λ°©λ²μ μ λͺ¨λ₯΄κ² μ΅λλ€. λ΄ button.js νμΌμ νμ¬ λ€μμ λ΄ λ³΄λ λλ€ ....
const Button = (props) => {
return (<stuff />)
}
export default React.memo(Button)
μλ₯Ό μ°Έμ‘°νμμμ€. https://github.com/zeit/next.js/issues/8962 μμ νμ μμ μ μ΄μμ΅λλ€.
Next JSλ₯Ό μ κ·Έλ μ΄λ ν λμ΄ λ¬Έμ κ° λ°μνμ΅λλ€. κ°λ κΈΈμ μμ μ΄ μμ΅λκΉ?
forwardRefλ₯Ό μ¬μ©νκ³ κ΅¬μ± μμλ₯Ό λννλ λ°©λ²μ μ λͺ¨λ₯΄κ² μ΅λλ€. λ΄ button.js νμΌμ νμ¬ λ€μμ λ΄ λ³΄λ λλ€ ....
const Button = (props) => { return (<stuff />) } export default React.memo(Button)
μ΄ κ²½μ°, λΉμ μ ν μ μμ΄μΌν©λλ€
import {forwardRef} from 'react';
const Button = forwardRef(props, ref) => {
return (<stuff ref={ref} />)
}
forwardRef λΆλΆ. λ¬Έμμλ https://nextjs.org/docs#forcing -the-link-to-expose-href-to-its-childκ° μΈκΈλμ΄ μμ§λ§ μλνμ§λ μμμ΅λλ€. λλ μ΄κ²μνλ κ²μ λ°κ²¬νλ€
<Link
href={href}
>
<a>
{text}
</a>
</Link>
μλν©λλ€ (λ§€μ° μ΄μν΄ λ³΄μμλ λΆκ΅¬νκ³ ).
9.0.2μμμ΄ λ¬Έμ λ°μ
<a></a>
μ¬μ©νλ©΄ μλ ν μ μμ§λ§ a11y λ° μ΄μ μ μ¬ν κ²μ μ¬μ©ν λ "must have href"λ±κ³Ό κ°μ λ€λ₯Έ μ€λ₯κ° λ°μν©λλ€.
νΈμ§ : μ΄κ²μ μμ λ΄κΈ° μν΄ 15 λΆμ 보λλλ°, κ·Έκ²μ λ§€μ° μ§λ£¨νκ³ μκ° λλΉμ΄λ©° κ²½κ³ μλ λΆκ΅¬νκ³ μ λλ‘ μλνλ κ² κ°μ΅λλ€.
κ°μΈμ μΌλ‘ https://github.com/zeit/next.js/issues/7915#issuecomment -511792629λ‘
λ°μ μ‘°κ° μ λ² μ΄λ§ λνΌλ‘ μ¬μ©νλμ΄ μ루μ μ μ΄λ»μ΅λκΉ?
import React, { Fragment, useRef } from 'react';
import { MyFunctionalComponent } from './my-functional-component';
const App = props => {
const myRef = useRef();
return (
<Link href={href}>
<Fragment>
<MyFunctionalComponent ref={myRef} />
</Fragment>
</Link>
);
}
<MyFunctionalComponent>
λ refλ₯Ό μ λ¬ν©λλ€.
import React, { forwardRef } from 'react';
const MyFunctionalComponent = forwardRef(props, ref) => (
<div ref={ref} />
);
μ΄λ μΆλ ₯ ( <div />
)μ μΆκ° HTML λνΌλ₯Ό μΆκ°νμ§ μμΌλ©° κ΄λ ¨ μ€λ₯λ₯Ό νΈλ¦¬κ±°νμ§ μμμΌν©λλ€.
@ hill84 ν₯λ―Έλ‘μ§ λ§ myRef
μΆμ²λ μ΄λμ
λκΉ?
@Vadorequest μ΄μ λκΈμ μμΈν λ΄μ©μ μΆκ°νμ΅λλ€.
@ hill84 μ μ μμ
```
const ButtonLink : FC
<
@ hill84 μ μ μμ
const ButtonLink: FC<ButtonLinkProps> = ({ children, href, ...props }) => ( <Link href={href}> <><Button link {...props}>{children}</Button></> </Link> );
<><Component/></>
νμ§λ§ Linkκ° μλνμ§ μμ΅λλ€. λμ <div></div>
λ₯Ό μ¬μ©ν΄μΌν©λλ€.
ν¨μκ° κ΅¬μ± μμ λμ μνμΌλ‘ κ°μ²΄λ₯Ό λ°ννλ μ루μ μ μ μν©λλ€.
const propsUserBtn = user => ({
className: "h-10 w-10 font-black rounded-full bg-orange-200 text-teal-700",
title: user.first_name + ' ' + user.last_name,
children: (user.first_name.charAt(0) + user.last_name.charAt(0)).toUpperCase()
})
<Link href='/profile'>
<button { ...propsUserBtn(user) } />
</Link>
νμΈ, μλν©λλ€!
μ΄ μ¬νμ΄ μ νμνκ°μ? passHref
κ° <Link />
μμ μ¬μ©λ λ κΈ°λ³Έ λν λ κ΅¬μ± μμμμ href
μ μ¬λ°λ₯Έ λ°°μΉλ₯Ό 보μ₯νλ κ²μ
λκΉ?
λ΄ μ¬μ©μ μ μ κ΅¬μ± μμκ° λ£¨νΈμ <a>
μκ³ μ΄μ¨λ hrefκ° μ¬λ°λ₯΄κ² μ μ©λκ³ μμμ μ μ μκΈ° λλ¬Έμ μ΄κ²μ λ§€μ° μ±κ°μ μΌ μ
λλ€. 루νΈμ μ΅μ»€κ° μκ³ μ΄ μ 체 ref
λμμΈ μ§μ μ νΌνμμμ€.
React.forwardRef
μ μ¬λλ€μ΄ ꡬννλλ‘ κ°μνκ³ λ΄ λ¦°ν°λ‘ μ§μ₯μ νλ μ΄νλ λμ°νκ² μ±κ°μ μΌμ
λλ€ π
λλ κ΅¬μ± μμμμ href
μ μμΉλ₯Ό ββμ§μ ν΄μΌνλ κ²½μ° μ μ΄λ μ΅νΈ μΈ κΈ°λ₯μ΄μ΄μΌν©λλ€. <Link href="/" passHref useRef />
?
μ°Έμ‘°λ₯Ό μ¬μ©μ μ§μ κ΅¬μ± μμλ‘ μ λ¬νκΈ° μν΄ λ€λ₯Έ λͺ¨λμ λ§λ€μ΄μΌνλ κ²μ μ’μ μ루μ μ΄λ©° λ무 λ°λ³΅μ μ΄λΌκ³ μκ°νμ§ μμ΅λλ€.
μ΄ λ¬Έμ λ₯Ό μ²λ¦¬νλ λ° μ¬μ©νλ λ°©λ²μ @ namlq93μμ μ μν κ²μ²λΌ div
μμλ‘ μ¬μ©μ μ§μ κ΅¬μ± μμλ₯Ό λννλ κ²μ΄μ§λ§ κΉ¨λνμ§ μμ κ² κ°μ΅λλ€.
μ¬κΈ°λ λ§μ°¬κ°μ§μ λλ€. λ€μκ³Ό κ°μ μ¬μ©μ μ§μ κ΅¬μ± μμλ₯Ό μ¬μ©νλ©΄ μ€λ₯κ° λ°μν©λλ€.
<Link href="/href"> <Button type="custom" color="#FCFCFC" > buttonText </Button> </Link>
μ΄λ κ΅¬μ± μμλ₯Ό μμμΌλ‘ μ¬μ©ν λλ§ λ°μν©λλ€. μλ₯Ό λ€μ΄ κ²½κ³ λ₯Ό μ 곡νμ§ μμ΅λλ€.
<Link href="/href"> <button type="custom" color="#FCFCFC" > buttonText </button> </Link> <Link href="/href"> <a> <Button type="custom" color="#FCFCFC" > buttonText </Button> </a> </Link>
μλν©λλ€, μΉκ΅¬ κ°μ¬ν©λλ€ !!
λΉμ·ν λ¬Έμ κ° μμ΅λλ€
μμ§ κ±°κΈ°
https://nextjs.org/docs/api-reference/next/link#if -the-child-is-a-function-component
μ΄κ±° μ¬νλ€.
μΌλ§μ μ NextJS 컨νΌλ°μ€λ₯Όλ³΄κ³ "μμ° λ₯μ€νΈκ° λ―Έλ λ€"λΌκ³ μκ°νμ΅λλ€. μ λ Nextμ Vercel μνκ³λ₯Ό μ λ§ μ’μνκ³ μμ§ν κ·Έλ κ² λ§ν μ μκ³ Nextμ μΉνΈμκ°λκ³ μΆμ΅λλ€. κ·Έλ¬λ μ΄μ κ°μ λ¬Έμ κ° μ’ κ²°λκ³ ν΄κ²°λμ§ μμΌλ©΄ κ·Έλ κ²νκΈ°κ° μ΄λ ΅μ΅λλ€. λλ νΉν λΌμ°ν°μ κ΄λ ¨λ λ¬Έμ μ 체μμ μ΄λ¬ν μ νμ ν¨ν΄μ μ¬λ¬ λ² λ³΄μμ΅λλ€.
μ¬μ©μ μΈ‘μμμ΄ λ¬Έμ μ λν 3 κ°μ§ ν΄κ²°μ± μ΄ μμΌλ©° κ·Έμ€ μ΄λ κ²λ νλ₯ν DXκ° μλλλ€.
React.forwardRef
κ΅¬μ± μμλ₯Ό λνν©λλ€. μ΄κ²μ λ§€μ° λ°λ³΅μ μΌ μ μμ΅λλ€.div
, span
, a
λ±κ³Ό κ°μ νκ·Έλ‘ κΈ°λ₯ κ΅¬μ± μμλ₯Ό λνν©λλ€.router.push
μ΄λ€ μ루μ μ μ¬μ©νλ λͺ¨λ μ΄μνλ©°μ΄ λ¬Έμ λλ λ¬Έμ μ λ§ν¬ λ μ£Όμμ΄ νμνμ¬ λλ£ / κΈ°μ¬μμκ² μ€μλ‘ μ κ±° / μ 리λ₯Ό λ°©μ§νλ λ° νμν μ΄μ λ₯Ό μ€λͺ ν΄μΌν©λλ€.
μ΄ λ¬Έμ λ λ Όμλ₯Ό μν΄ μ¬κ°λμ΄μΌνλ€κ³ μκ°ν©λλ€.
κ°μ₯ μ μ©ν λκΈ
Link
λ΄μμ μ¬μ©λλ λͺ¨λ κΈ°λ₯ κ΅¬μ± μμλ₯Ό μλμΌλ‘ λν νμκ² μ΅λκΉ? μ΄λ₯Όμν λ μ½κ³ λ λ°λ³΅μ μΈ μ루μ μ΄ μμ΄μΌνμ§ μμ΅λκΉ? μ μ΄λ<Link href="/" forwardRef>...</Link>
?