我正在使用s styled component返回material ui Fab组件,并且在控制台中得到以下错误:
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下面是组件:
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)};
`;我知道为什么会出现这个警告,所以我设法将其更改为,
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警告抛出:
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中不是必需的。
发布于 2020-09-30 18:46:03
它有点臃肿,但实际上是推荐的方式:
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道具。
https://stackoverflow.com/questions/64135464
复制相似问题