材料UI+用道具抛出的样式“ React”无法识别DOM元素上的Zindex Prop。

发布于 2025-01-26 14:06:11 字数 481 浏览 2 评论 0原文

我正在努力争论错误 - > React无法在DOM元素上识别Zindex Prop。 当我使用MUI组件并用道具设计时,它会发生。

代码示例:

样式:

import {Drawer as DrawerBase, styled} from '@material-ui/core'

interface DrawerProps {
  zIndex: number
}
export const Drawer = styled(DrawerBase)<Theme, DrawerProps>(({zIndex}) => ({
  zIndex
}))

实现:

<Drawer zIndex={10} />

我正在使用: “@Material-ui/core”:“^4.12.3”

是否有可能防止将道具传递给DOM元素?

I'm struggling with React error -> React does not recognize zIndex prop on a DOM element.
It happens when I'm using MUI component and Styled with props.

Code example:

styled:

import {Drawer as DrawerBase, styled} from '@material-ui/core'

interface DrawerProps {
  zIndex: number
}
export const Drawer = styled(DrawerBase)<Theme, DrawerProps>(({zIndex}) => ({
  zIndex
}))

implementation:

<Drawer zIndex={10} />

I'm using:
"@material-ui/core": "^4.12.3"

Is there any possibility to prevent passing props to DOM elements?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

全部不再 2025-02-02 14:06:11

不幸的是,材料UI的“样式”语法令人困惑。但是,这应该起作用:

interface DrawerProps {
  zIndex: number
}

export const Drawer = styled(DrawerBase, {
  shouldForwardProp: (prop) => prop !== 'zIndex',
})<Theme, DrawerProps>(({zIndex}) => ({
  zIndex
}))

“应该ForwardProp”也是一个不幸的名称,因为听起来像属性Zindex并未转发,但实际上可以访问。

希望它有帮助。

Material UI's 'styled' syntax is very confusing unfortunately. This should work though:

interface DrawerProps {
  zIndex: number
}

export const Drawer = styled(DrawerBase, {
  shouldForwardProp: (prop) => prop !== 'zIndex',
})<Theme, DrawerProps>(({zIndex}) => ({
  zIndex
}))

'shouldForwardProp' is also an unfortunate name as it sounds like the property zIndex isn't forwarded, but it is actually accessible.

Hope it helps.

风吹短裙飘 2025-02-02 14:06:11

我以前从未在MUI中使用过样式的组件,所以请用一粒盐来做什么。但是我有一些想法:

您是从'@youteral-ui/core'导入样式,但是您正在调用style> style功能(没有“ D”)。

查看 MUI文档使用样式组件使用它的方式,我们传统上使用了使用CSS语法的样式组件。我希望您的代码看起来像这样:

const Drawer = styled(DrawerBase)`
  z-index: ${props => props.zIndex};
`;

DOM元素需要“ z index”,而不是“ zindex”。

您的代码段还具有带有您的const抽屉的错字,其中您已经排除了“ S”:cont

希望这会有所帮助。

I've not used styled components within MUI before, so take what I'm saying with a grain of salt. But I have a few ideas:

You're importing styled from '@material-ui/core', but you're calling a style function (without the "d").

Looking at the MUI docs for using styled components, they're using it the way we traditionally use styled components, which is using CSS syntax. I'd expect your code to look like this:

const Drawer = styled(DrawerBase)`
  z-index: ${props => props.zIndex};
`;

The DOM element needs "z-index", not "zIndex".

Your code snippet also has a typo with your const Drawer where you've left out the "s": cont.

Hopefully this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文