【发布时间】:2021-09-11 06:45:04
【问题描述】:
我正在使用 TypeScript(v4.2.3) 和 Material UI(v4.11.3),并尝试将自定义道具传递给 styled 组件。
import React from 'react';
import {
IconButton,
styled,
} from '@material-ui/core';
const NavListButton = styled(IconButton)(
({ theme, panelOpen }: { theme: Theme; panelOpen: boolean }) => ({
display: 'inline-block',
width: 48,
height: 48,
borderRadius: 24,
margin: theme.spacing(1),
backgroundColor: panelOpen ? theme.palette.secondary.dark : 'transparent',
}),
);
...
const Component: React.FC<Props> = () => {
return (
<NavListButton panelOpen={true} />
);
};
这工作正常,但如果我检查控制台,它会遇到错误。
Warning: React does not recognize the `panelOpen` prop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell it as lowercase `panelopen` instead. If you accidentally passed it from a parent component, remove it from the DOM element.
at button
at ButtonBase ...
如何在不使用 styled-components 模块的情况下摆脱这种情况,需要使用 Material UI 中的styled?
这也是另一个类似的问题。
我想向样式化组件添加媒体查询,上面写着TypeError: Cannot read property 'addRule' of null。
const PrintableTableContainer = styled(TableContainer)(() => ({
'@media print': {
'@page': { size: 'landscape' },
},
}));
我也试过了。
const PrintableTableContainer = styled(TableContainer)`
@media print {
@page { size: landscape }
}
`;
但它也说
Argument of type 'TemplateStringsArray' is not assignable to parameter of type 'CreateCSSProperties<(value: string, index: number, array: readonly string[]) => unknown> | ((props: { theme: Theme; } & ((value: string, index: number, array: readonly string[]) => unknown)) => CreateCSSProperties<...>)'.
Type 'TemplateStringsArray' is not assignable to type 'CreateCSSProperties<(value: string, index: number, array: readonly string[]) => unknown>'.
Types of property 'filter' are incompatible.
...
【问题讨论】:
-
无法摆脱这个?
标签: typescript material-ui styled-components