Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

styled-components withTheme HOC not working with types React.FC

I'm building some components with React.FC typescript and today I found this typescript error when trying to inject styled-component props using withTheme HOC from styled-components:

enter image description here

It seems that withTheme HOC only accepts React.ComponentType as parameter, but component was build using React.FC (Functional Component).

Is there a way to cast React.FC to React.ComponentType?

UPDATE

The full component implementation:

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)
like image 667
Andrew Ribeiro Avatar asked Dec 18 '22 15:12

Andrew Ribeiro


1 Answers

You need to add a theme prop to your component props interface:

interface Theme {}

export interface ILoadingBarComponent {
  progress: number
  appearance?: string
  onFinish(finished: Promise<string>): void
  theme: Theme
}
like image 74
Hamza El Aoutar Avatar answered Dec 28 '22 16:12

Hamza El Aoutar