gpt4 book ai didi

javascript - 样式组件 withTheme HOC 不适用于 React.FC 类型

转载 作者:行者123 更新时间:2023-11-30 09:11:32 28 4
gpt4 key购买 nike

我正在使用 React.FC typescript 构建一些组件,今天我在尝试使用 withTheme HOC 从 styled-components 注入(inject) styled-component props 时发现了这个 typescript 错误:

enter image description here

似乎 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)

最佳答案

你需要在你的组件 props 接口(interface)中添加一个 theme prop:

interface Theme {}

export interface ILoadingBarComponent {
progress: number
appearance?: string
onFinish(finished: Promise<string>): void
theme: Theme
}

关于javascript - 样式组件 withTheme HOC 不适用于 React.FC 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58542056/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com