reactjs - 使用 styled-components 进行样式设置时,输入 React Components 的最佳方法是什么
问题描述
我正在将一个项目迁移到 Typescript。我有一个使用 styled-components 设置样式的组件,我需要在其他组件上使用 styled-components 覆盖它。我的组件看起来像这样。
该组件旨在渲染图像,如果图像不存在,则返回使用 styled-components 样式的 Fallback 元素。
import React, { useState } from 'react'
import styled from 'styled-components'
export const Fallback = styled.div`
padding-bottom: 56%; /* 16/9 ratio */
background-color: #e5e5e5;
`
export const Image = styled.img`
image-rendering: pixelated;
image-rendering: crisp-edges;
`
interface ImageWithFallbackProps {
src: string
alt: string
fallback: typeof Fallback
}
const ImageWithFallback = ({
src,
alt,
fallback = Fallback,
...props
}: ImageWithFallbackProps): React.ReactNode => {
const [error, setError] = useState(false)
const FallbackElement: React.FC = fallback
if (error) return <FallbackElement />
return <Image src={src} alt={alt} {...props} onError={() => setError(true)} />
}
export default ImageWithFallback
而且,我在另一个组件上使用它。
const Content: AnyStyledComponent = styled.div`
/* styles */
`
Content.Text = styled.div`
font-weight: bold;
flex: 1;
text-align: left;
`
Content.Image = styled(ImageWithFallback)`
flex: 1;
max-width: 80px;
align-self: baseline;
`
Content.Fallback = styled(Fallback)`
width: 80px;
padding-bottom: 54px;
`
但是我在这样定义 Content.Image 时遇到错误Content.Image = styled(ImageWithFallback)
。错误代码是 ts(2769)。我该如何解决这个问题?
解决方案
推荐阅读
- apache-spark - 将 PySpark 数据帧写入分区 Hive 表
- python - 使用几何通过 python 为 Abaqus 创建元素集
- c - 将用read()读取的数据写入C中的int数组
- macos - SwiftUI - 在 macOS 上翻转全局坐标空间
- extjs-grid - ExtJS 7.x 现代,如何从网格拖放到网格树?
- python - 如何使用 python 从 csv 文件中过滤出可用数据?
- asp.net - 是否可以将项目从 VS 2010 带到 VS 2019
- android - Jetpack Compose - LazyColumnFor 在添加项目时不重新组合
- python - Django ModelForm DateTimeInput 中的 input_formats 抛出 TypeError
- python - Pandas 将样式设置为 excel“TypeError:第一个参数必须是可调用的”