首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用样式化组件和材质UI时的Typescript和eslint问题:` `React无法识别DOM元素上的`showText`属性`

使用样式化组件和材质UI时的Typescript和eslint问题:` `React无法识别DOM元素上的`showText`属性`
EN

Stack Overflow用户
提问于 2020-09-30 18:17:16
回答 1查看 453关注 0票数 2

我正在使用s styled component返回material ui Fab组件,并且在控制台中得到以下错误:

代码语言:javascript
复制
React does not recognize the `showText` prop on a DOM element.
  If you intentionally want it to appear in the DOM as a custom attribute, spell it as lowercase `showtext` instead

下面是组件:

代码语言:javascript
复制
import styled from 'styled-components';
import MuiFab from '@material-ui/core/Fab';

type TFabContainer = {
    showText: boolean;
}
const FabContainer = styled(MuiFab)<TFabContainer>`
    width: 250px;
    animation-duration: ${({ showText }) => (showText ? '0.25s' : '1s')};
    animation-iteration-count: linear;
    animation-name: ${({ showText }) => (showText ? expand : contract)};
`;

我知道为什么会出现这个警告,所以我设法将其更改为,

代码语言:javascript
复制
import styled from 'styled-components';
import MuiFab, { FabProps } from '@material-ui/core/Fab';

type TFabContainer = {
    showText: boolean;
    rest: FabProps;
}
const FabContainer = styled(({ showText, ...rest }: TFabContainer) => <MuiFab {...rest} />)`
    width: 250px;
    animation-duration: ${({ showText }) => (showText ? '0.25s' : '1s')};
    animation-iteration-count: linear;
    animation-name: ${({ showText }) => (showText ? expand : contract)};
`;

这消除了错误,并正确地呈现了我的组件与预期的行为,但现在我得到了2个lint和TS警告抛出:

代码语言:javascript
复制
const FabContainer = styled(({ showText, ...rest }: TFabContainer) => <MuiFab {...rest} />)`
                               ^^^^^^^^                                ^^^^^^
'showText' is defined but never used ↑                                  ↑ Property 'href' is missing in type '{ rest: OverrideProps<FabTypeMap<{}, "button">, "button">; }' but required in type '{ href: string; }'.

这些错误以前不在这里。也不确定href属性从何而来,这在MuiFab中不是必需的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-30 18:46:03

它有点臃肿,但实际上是推荐的方式:

代码语言:javascript
复制
const FabContainer = styled(({ showText, ...rest }: TFabContainer & Omit<MuiFabProps, keyof TFabContainer>)) => <MuiFab {...rest} />)({
    width: 250px;
    animation-duration: ${({ showText }) => (showText ? '0.25s' : '1s')};
    animation-iteration-count: linear;
    animation-name: ${({ showText }) => (showText ? expand : contract)};
});

这样,您就可以告诉编译器您的道具不会被用作DOM道具。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64135464

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档