Javascript 如何转发ref函数声明而不是arrow函数?
以下是React网站中的示例:Javascript 如何转发ref函数声明而不是arrow函数?,javascript,reactjs,Javascript,Reactjs,以下是React网站中的示例: const FancyButton=React.forwardRef((props,ref)=>( {props.children} )); 如何对函数(){}执行相同的操作?我之所以要这样做,是因为我想避免创建匿名函数来帮助调试。您可以将React函数组件传递给React.forwardRef的参数 function Button(props, ref) { return ( <button ref={ref} className=&qu
const FancyButton=React.forwardRef((props,ref)=>(
{props.children}
));
如何对
函数(){}
执行相同的操作?我之所以要这样做,是因为我想避免创建匿名函数来帮助调试。您可以将React函数组件传递给React.forwardRef的参数
function Button(props, ref) {
return (
<button ref={ref} className="FancyButton">
{props.children}
</button>
)
}
const FancyButton = React.forwardRef(Button);
功能按钮(道具,参考){
返回(
{props.children}
)
}
const FancyButton=React.forwardRef(按钮);
const FancyButton=React.forwardRef(
功能按钮(道具,参考){
返回(
{props.children}
)
}
);
只需像平常一样声明函数,并将其传递给forwardRef,而不是内联箭头函数。
const FancyButton = React.forwardRef(
function Button(props, ref) {
return (
<button ref={ref} className="FancyButton">
{props.children}
</button>
)
}
);