简体   繁体   English

MUI v5 + styled() + ListItemButton:属性“to”/“component”不存在

[英]MUI v5 + styled() + ListItemButton: property 'to'/'component' does not exist

In the migration from MUI v4 to v5 I'm hitting this road block: in v4 I've used makeStyles() , but now want to fully migrate to styled() : I can't get Typescript to accept a styled(ListItemButton)(...) with to= and component= properties.在从 MUI v4 到 v5 的迁移中,我遇到了这个障碍:在 v4 中我使用了makeStyles() ,但现在想完全迁移到styled() :我无法让 Typescript 接受styled(ListItemButton)(...)具有to=component=属性。

I've seen and read MUI's guide on Wrapping components which actually made things even less clear to me;我已经看过并阅读过 MUI 的Wrapping components指南,这实际上让我更不明白; admittedly, I'm neither a Typescript nor MUI wizard.诚然,我既不是 Typescript 也不是 MUI 向导。 My confusion is fueled by the guide's examples being incomplete, such as obviously lacking some non-obviously imports which seem to need imported symbol renaming, so automatic completion won't work or turn up multiple candidates.指南的示例不完整加剧了我的困惑,例如显然缺少一些似乎需要导入符号重命名的非明显导入,因此自动完成将不起作用或出现多个候选。

This is a minimized example triggering the Typescript error, see below for codesandbox link.这是触发 Typescript 错误的最小化示例,请参阅下面的代码和框链接。

import React from "react"
import {
  Avatar,
  Link,
  ListItemAvatar,
  ListItemButton,
  styled
} from "@mui/material"

const ListItemButtonLink = styled(ListItemButton)(({ theme }) => ({
  // ...here be styling
}))

interface LinkedListItemProps {
  path: string
}

export const LinkedListItem = ({ path }: LinkedListItemProps) => {
  return (
    <ListItemButtonLink key={42} dense to={path} component={Link}>
      <ListItemAvatar>
        <Avatar>A</Avatar>
      </ListItemAvatar>
      Here Be Item
    </ListItemButtonLink>
  )
}

I'm totally out in the dark in how to get this working, as the Guide example doesn't pass Typescript checking either.我完全不知道如何让它工作,因为 Guide 示例也没有通过 Typescript 检查。

Looking through the MUI issues I found an issue that tackled the Guide issue, but doesn't really seem to fix it in a way that I could use.查看 MUI 问题,我发现一个问题解决了 Guide 问题,但似乎并没有真正以我可以使用的方式修复它。

I've also seen and read MUI button with styled-components including react router Link , but the solution is basically the non-Typescript guide version of a kind.我还看到并阅读了带有样式组件的 MUI 按钮,包括 react router Link ,但解决方案基本上是一种非 Typescript 指南版本。

编辑样式列表项按钮 (updated) (更新)

You're using Link from the MUI package, this Link is just an anchor element but with better style integration with MUI theme, The Link you might want to use is from react-router which has a to prop, so change your code to:您使用Link从MUI包,这个Link只是一个锚元素,但与MUI主题更好的风格融合, Link您可能希望使用是具有反应路由器to支撑,所以更改您的代码:

import { Link } from "react-router-dom";

If you use styled and want the resulted component to have the prop to from react-router, you can add the generic type parameter to the HOC:如果你使用styled ,并希望得到成分有道具to从反应的路由器,可以将泛型类型参数添加到HOC:

import { Link, LinkProps } from "react-router-dom";

const ListItemButtonLink = styled(ListItemButton)<LinkProps>(({ theme }) => ({
  // ...here be styling
}));

代码沙盒演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM