hot*_*dev 6 typescript material-ui styled-components
我正在使用 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} />
);
};
Run Code Online (Sandbox Code Playgroud)
这工作正常,但如果我检查控制台,它会遇到错误。
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 ...
Run Code Online (Sandbox Code Playgroud)
如何在不使用样式组件模块的情况下摆脱这个问题,需要styled
从 Material UI 中使用?
这也是另一个类似的问题。
我想将媒体查询添加到样式组件中,它说TypeError: Cannot read property 'addRule' of null
。
const PrintableTableContainer = styled(TableContainer)(() => ({
'@media print': {
'@page': { size: 'landscape' },
},
}));
Run Code Online (Sandbox Code Playgroud)
我也尝试过这个。
const PrintableTableContainer = styled(TableContainer)`
@media print {
@page { size: landscape }
}
`;
Run Code Online (Sandbox Code Playgroud)
但它也说
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.
...
Run Code Online (Sandbox Code Playgroud)
只是我的2分钱,希望我提供的解决方案可以帮助你。
主要原因是material-ui版本,你的项目可能会混淆版本4和版本5。
第一:安装material-ui v5
@next
版本=v5// npm
npm install @material-ui/core@next @material-ui/styled-engine-sc@next styled-components
npm install --save-dev @types/styled-components
// yarn
yarn add @material-ui/core@next @material-ui/styled-engine-sc@next styled-components
yarn add --dev @types/styled-components
Run Code Online (Sandbox Code Playgroud)
第二:安装react-app-rewired
默认的material-uistyled-engine
指向的是emotion
,所以我们需要重写webpack配置来引用styled-components。
官方文档解释了如何在material-ui中使用styled-components:
对于create-react-app
,安装react-app-rewired
// npm
npm install --save-dev react-app-rewired customize-cra
// yarn
yarn add --dev react-app-rewired customize-cra
Run Code Online (Sandbox Code Playgroud)
第三:覆盖 webpack 配置
config-overrides.js
// npm
npm install @material-ui/core@next @material-ui/styled-engine-sc@next styled-components
npm install --save-dev @types/styled-components
// yarn
yarn add @material-ui/core@next @material-ui/styled-engine-sc@next styled-components
yarn add --dev @types/styled-components
Run Code Online (Sandbox Code Playgroud)
第四:现在您可以将material-ui与styled-components一起使用
// npm
npm install --save-dev react-app-rewired customize-cra
// yarn
yarn add --dev react-app-rewired customize-cra
Run Code Online (Sandbox Code Playgroud)
对于第一个问题,React does not recognize the `panelOpen` prop on a DOM element.
.
styled-components 提供了瞬态 props -$
以避免将自定义 props 传递到 DOM。
https://styled-components.com/docs/api#transient-props
一个演示 git 存储库,供您参考和交叉检查,以防您无法使其工作
归档时间: |
|
查看次数: |
8672 次 |
最近记录: |