[英]Typescript React - Type 'string' is not assignable to type 'Appearance'
I'm trying to create some UI utility components with React Typescript.我正在尝试使用 React Typescript 创建一些 UI 实用程序组件。 These utilities are meant to provide some default styling to HTML elements.
这些实用程序旨在为 HTML 元素提供一些默认样式。
FullScreen
is just a div whose height is 100vh
and whose width is 100vw
. FullScreen
只是一个高度为100vh
且宽度为100vw
的 div。
So FullSreenProps
extends HTMLDivElement
.所以
FullSreenProps
扩展HTMLDivElement
。 But when I spread props.style
into the style property, I get a type error:但是当我将
props.style
传播到 style 属性中时,我得到一个类型错误:
Type '{ width: string; height: string; accentColor: string; alignContent: string; alignItems: string; alignSelf: string; alignmentBaseline: string; all: string; animation: string; animationDelay: string; ... 443 more ...; [Symbol.iterator](): IterableIterator<...>; }' is not assignable to type 'CSSProperties'.
Types of property 'appearance' are incompatible.
Type 'string' is not assignable to type 'Appearance'.ts(2322)
Here's the FullScreen
component:这是
FullScreen
组件:
import React, { useState, useEffect, useMemo } from 'react';
interface FullScreenProps extends HTMLDivElement{}
const FullScreen = (props: FullScreenProps): JSX.Element => {
return (
<div
{...props}
style={
{
...props.style,
width: "100vw",
height: "100vh",
}
}
>
</div>
);
}
export default FullScreen
What's the correct way to pass props.style
while also specificying height: 100vh
and width: 100vw
in the style
prop?传递
props.style
的正确方法是什么,同时在style
属性中指定height: 100vh
和width: 100vw
?
You can try the following:您可以尝试以下方法:
import React from 'react';
interface FullScreenProps extends Omit<React.HTMLProps<HTMLDivElement>, 'as' | 'ref'> {}
const FullScreen = (props: FullScreenProps): JSX.Element => {
return (
<div
{...props}
style={
{
...(props.style || {}),
width: "100vw",
height: "100vh",
}
}
>
</div>
);
}
export default FullScreen
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.