0

I tried to create a React HOC but unfortunately an error thrown as written below

Argument of type 'Element' is not assignable to parameter of type 'ComponentType<unknown>'.

The code written below,

import React from 'react';
import { Link } from 'react-router-dom';

export default function enrich<P>(WrappedComponent: React.ComponentType<P>) {
  function Enrich(props: P) {
    return <WrappedComponent {...props} />;
  }
  return Enrich;
}

enrich(<Link to="#" />);

How could I create a React HOC written in Typescript?

1 Answer 1

3

The Argument you are passing to the enrich function needs to be a component. If you hover over it it will probably show you something like:

type React.ComponentType<P = {}> = React.ComponentClass<P, any> | React.FunctionComponent<P>

this shows you that the element (instantiated react element) is not matching the type. You could pass a function wrapping the Link element for example like this:

enrich((props) => <Link to="#" {...props} />);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.