我正在将我的应用程序移动到 Material UI V4,当以编程方式设置“to”props时,我正在努力将我的react-router链接组件移动到 forwardRef 包装的组件中。
下面的代码有效,但需要复制对 forwardRef 的调用并构建 props 对象,我更愿意在一个用参数调用一次的函数中完成这项工作,但我不知道如何去做。
const ViewLink = (props, ref) => {
console.log(props);
switch (props.type) {
case 'entities':
return <Link to={`/entities/${props.id}`} {...props} innerRef={ref} />;
case 'templates':
return <Link to={`/templates/${props.id}`} {...props} innerRef={ref} />;
default:
return null;
}
}
<Button
className={classes.buttonFont}
component={React.forwardRef((props, ref) => ViewLink(
{
id: childData.id,
type: type,
...props
},
ref
))}
>
{childData[column]}
</Button>
有没有办法创建一个处理 switch 语句和 forwardRef 的函数?理想情况下,如下所示:
<Button
className={classes.buttonFont}
component={(props) => ViewLink(id, type, props)}
>
{childData[column]}
</Button>