Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Injecting props through higher order component causes compiler error

I am trying to inject props into a component using a higher order component (hoc). I follow along this article. This is my hoc:

// WithWindowSize.tsx
import React, {useEffect, useMemo, useState} from 'react'

interface WindowSize {
    width: number,
    height: number
}

export function WithWindowSize <T>(Component: React.ComponentType<T>) {
    return function WithComponent(props: Omit<T, "width" | "height">) {
        const [windowSize, setWindowSize] = useState({} as WindowSize)

        useEffect(() => {
            ...
        }, [])


        return <Component
            {...(props as T)}
            windowSize={windowSize}/>
    }
}

export default WithWindowSize;

And this is how I try to use the hoc WithWindowSize

// Foo.tsx
interface FooProps {
    headline: string,
    value: string | number,
}

const Foo = ({headline, value, windowSize}: FooProps) => {
    return ...
}

export default WithWindowSize(Foo);

But, this highlights the prop windowSize in <Foo />, and tells me

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Property ‘windowSize’ does not exist on type ‘FooProps’.

Why is that?

>Solution :

Define windowSize prop in interface you have defined

interface FooProps {
    headline: string,
    value: string | number,
    windowSize: string | number
}

I also see there is one extra O in type FoooProps you have given is it some typo or other type than FooProps ?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading