React TypeScript 样式报错

代码如下:

报错内容:

Type '{ flexDirection: string; }' is not assignable to type 'Properties<string | number, string & {}>'.

Types of property 'flexDirection' are incompatible.

Type 'string' is not assignable to type 'FlexDirection | undefined'.ts(2322)

index.d.ts(1942, 9): The expected type comes from property 'style' which is declared here on type 'DetailedHTMLProps<HTMLAttributes, HTMLDivElement>'

解决方案 1:

typescript 复制代码
const myStyle = {
  flexDirection: "column",
} as React.CSSProperties;

解决方案 2:

typescript 复制代码
class MyView extends Component {
  render() {
    return <div style={myStyle as React.CSSProperties}>my view</div>;
  }
}
相关推荐
黄林晴1 小时前
第一次听到 Tauri 这个词,去学习一下
前端
可可爱爱的你吖1 小时前
蜂鸟云地图简单实现
前端
布局呆星1 小时前
Vue3 :生命周期、DOM 操作与自定义组合式函数
前端·javascript·vue.js
147API1 小时前
多模型路由规则设计实战:第一版系统别做成黑盒
服务器·前端·javascript
xcjbqd01 小时前
CSS中隐藏元素的多重技巧与应用场景
前端·css
chenbin___1 小时前
检查hooks依赖的工具(转自千问)
开发语言·前端·javascript·react native·react.js
垚森1 小时前
我用AI写了一个颜值拉满的桌面媒体播放器,全程没动一行代码,这就是AI编程新范式
ai·electron·react·opencode
阿凤211 小时前
uniapp运行到app端怎么打开文件
android·前端·javascript·uni-app
久爱@勿忘2 小时前
vue/uniapp H5页面截图
开发语言·前端·javascript