5 typescript reactjs styled-components
我对如何在.attrs()TypeScript 中使用该函数感到有些困惑。说我有以下几点:
BottleBar.tsx:
interface IBottleComponentProps {
fill?: boolean
}
const BottleComponent = styled.div.attrs<IBottleComponentProps>(({fill}) => ({
style: {
backgroundImage: `url("./media/images/${fill ? 'Bottle-Filled.png' : 'Bottle-Empty.png'")`
}
}))<IBottleComponentProps`
width: 20px;
height: 20px;
`;
export default function BottleBar() {
return (
<Wrapper>
<BottleComponent />
<BottleComponent fill />
</Wrapper>
)
}
Run Code Online (Sandbox Code Playgroud)
现在,上面的代码有效,但我不确定为什么IBottleComponentProps在开头和结尾都需要两次 - 没有它,我得到以下内容:
Type '{ fill: boolean; }' is not assignable to type 'IntrinsicAttributes & Pick<Pick<Pick<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "slot" | ... 253 more ... | "onTransitionEndCapture"> & { ...; }, "slot" | ... 254 more ... | "onTransitionEndCapture"> & Partial<...>, "slot" | ... 254 more ... | "onTransitionEndCapture"> & { ...; } & { ...; }'.
Run Code Online (Sandbox Code Playgroud)
此外,在第一个代码示例中,我得到了一个浏览器日志;
index.js:1 Warning: Received `true` for a non-boolean attribute `fill`.
Run Code Online (Sandbox Code Playgroud)
老实说,这很令人困惑,而且 Styled-Components 文档在这方面不是很清楚。朝着正确的方向推动将不胜感激。
Warning about fill您需要为您的样式组件选择一个不同的名称,也许是full,但不是fill。Asfill是一些 HTML 元素的标准属性。此外,在 w3schools
如果您声明fill为string并传递一个字符串值,您可以看到在HTML DOM 中fill添加了一个属性,例如:div
<div
fill="test"
style="background-image: url("/media/images/image_file.png");" class="sc-AxiKw jDjxaQ">
</div>
Run Code Online (Sandbox Code Playgroud)
fill 是 SVGAttributes 接口中的一个属性:来自node_modules/@types/react/index.d.ts:
interface SVGAttributes<T> extends AriaAttributes, DOMAttributes<T> {
// Attributes which also defined in HTMLAttributes
className?: string;
id?: string;
...
// SVG Specific attributes
accentHeight?: number | string;
...
fill?: string;
...
}
Run Code Online (Sandbox Code Playgroud)
这就是这个警告的原因:
Warning: Received `true` for a non-boolean attribute `fill`.
If you want to write it to the DOM, pass a string instead: fill="true" or fill={value.toString()}.
Run Code Online (Sandbox Code Playgroud)
Why interface is required 2 times?以下是相关界面的摘录:
attrs<
U,
NewA extends Partial<StyledComponentPropsWithRef<C> & U> & {
[others: string]: any;
} = {}
>(
attrs: Attrs<StyledComponentPropsWithRef<C> & U, NewA, T>
): ThemedStyledFunction<C, T, O & NewA, A | keyof NewA>;
Run Code Online (Sandbox Code Playgroud)
Ubecome :IBottleComponentProps您传递的
C是 HTML 元素或 react 组件类型
返回类型是ThemedStyledFunction<C, T, O & NewA, A | keyof NewA>:
export interface ThemedStyledFunction<
C extends keyof JSX.IntrinsicElements | React.ComponentType<any>,
T extends object,
O extends object = {},
A extends keyof any = never
Run Code Online (Sandbox Code Playgroud)
其中C,T已经提供。您提供O通过传递IBottleComponentProps的第2次。
如果您不提供它,您BottleComponent将看起来像下面{}的道具,即没有道具:
如果您提供,它将看起来像下面一个,带有正确的道具。
简而言之,您现在必须提供 2 次接口。any如果您没有定义接口,您可以提供。
| 归档时间: |
|
| 查看次数: |
3195 次 |
| 最近记录: |