我正在使用React.FC打字稿构建一些组件,今天,当我尝试使用withTheme
HOC从样式组件中注入样式组件props时,发现了这个打字稿错误:
似乎withTheme
HOC仅接受React.ComponentType
作为参数,但是组件是使用React.FC
(功能组件)构建的。
是否可以将React.FC
强制转换为React.ComponentType
?
更新
完整的组件实现:
import React, { useEffect } from 'react'
import PropTypes from 'prop-types'
import { Reset, LoadingBarStyled, SpinnerContainer } from './Style'
import { withTheme } from 'styled-components'
import ScaleLoader from 'react-spinners/ScaleLoader'
export interface ILoadingBarComponent {
progress: number
appearance?: string
onFinish(finished: Promise<string>): void
}
const LoadingBarComponent: React.FC<ILoadingBarComponent> = ({
progress = 0,
appearance = 'default',
onFinish
}) => {
useEffect(() => {
if (progress >= 100 && onFinish) {
onFinish(
new Promise((resolve, reject) => {
setTimeout(() => {
resolve('finished')
}, 800)
})
)
}
}, [progress, onFinish])
return (
<div className="loading-bar-component-module">
<Reset />
{progress > -1 && progress < 101 && (
<>
<LoadingBarStyled progress={progress} appearance={appearance} />
<SpinnerContainer progress={progress}>
<ScaleLoader height={10} />
</SpinnerContainer>
</>
)}
</div>
)
}
LoadingBarComponent.propTypes = {
progress: PropTypes.number.isRequired,
appearance: PropTypes.string,
onFinish: PropTypes.func.isRequired
}
export default withTheme(LoadingBarComponent)
答案 0 :(得分:2)
您需要在组件props界面中添加一个theme
prop:
interface Theme {}
export interface ILoadingBarComponent {
progress: number
appearance?: string
onFinish(finished: Promise<string>): void
theme: Theme
}