3

I have an object which contains a reference to a FunctionalComponent however when I pass that reference into the createElement statement it is throwing a TS error.

Question: why is this complaining? my interface excepts both Component and FunctionalComponent.

error-message

Example:

const someFunctionalComponent: React.FC = () => {
  return <div></div>
}
const anotherFunctionalComponent: React.FC = () => {
  return <div></div>

}

const somePages = [someFunctionalComponent, anotherFunctionalComponent];

interface TabPage {
  active: number;
  content: React.Component | React.FC;
}


somePages.forEach(page => React.createElement(page))

1 Answer 1

9

You are getting confused between the component instance and the component class. The name of a class like React.Component represents a the type for an instance of that class. React.createElement is looking for a constructor rather than an instance.

The type for the class constructor is React.ComponentClass. You could use content: React.ComponentClass | React.FC. But there is actually a built-in helper for this union which is React.ComponentType<Props>.

interface TabPage {
  active: number;
  content: React.ComponentType;
}

Typescript Playground Link

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.