reactjs - 如何在反应中使用打字稿在div中传递样式
问题描述
这是我的代码我只想制作一个自定义组件来与打字稿做出反应,比如如果我想传递高度、宽度、边框半径和样式作为额外参数,比如如果我想要更多 css 属性然后传递 style={{display:flex}但我不能用打字稿添加这个我成功地用js写了它可以工作但不熟悉打字稿
我的界面
interface BoxProps {
heights: React.CSSProperties;
styles?: React.CSSProperties;
className?: String;
borderRadius?: React.CSSProperties
children?: React.ReactElement<any>
}
const Box = ({ id, heights, styles, className, children }: BoxProps) => {
const Nstyle = { ...styles,height: { heights } }
return (
<div
{...id}
style={Nstyle}
className={CN} >
{children}
</div >
);
}
请帮助样式使用
The expected type comes from property 'style' which is declared here on type
typescript 'DetailedHTMLProps, HTMLDivElement>' 显示此错误`
解决方案
道具heights
需要是string | number
. 该className
道具使用大写S
字母String
代替string
. className={CN}
应该是className={className}
因为变量CN
不存在。
interface BoxProps {
heights: string | number;
styles?: React.CSSProperties;
className?: string;
borderRadius?: React.CSSProperties
children?: React.ReactElement<any>
}
const Box = ({ heights, styles, className, children }: BoxProps) => {
const Nstyle: React.CSSProperties = { ...styles, height: heights }
return (
<div
style={Nstyle}
className={className}
>
{children}
</div >
);
}
推荐阅读
- php - 这个 Laravel 8 应用程序中“路由未定义”错误的原因是什么?
- c++ - 从 char[256] 属性的访问器返回 char[256]
- html - 如何从 ASP.NET MVC 中的 @Html.Raw 获取 href 的所有信息?
- python - 如何在 django 中部署基于 Experta 规则的系统
- azure - 具有以下要求的安全 VDI 环境 (WVD/Citrix)
- flutter - Flutter Firebase Phone 身份验证仅适用于 Android,不适用于 iOS
- c - C语言中的位数据类型
- php - JSON 解析错误:警告:尝试读取字符串上的属性
- javascript - 向 websocket 服务器 React Native 应用程序发送/写入值
- python - 从左到右产生