4

Here's the code sample and the JS version is working correctly.

The issue I encountered happening in TS :

import React from 'react'

class Parent extends React.Component {
    render() {
      return (
        <Child Cpnt={<Header title='Header' />} />
        /* TS error on Cpnt */
      )
    }
  }

interface ChildProps {
    Cpnt: React.ComponentClass
}

class Child extends React.Component <ChildProps> {
    render () {
        const { Cpnt } = this.props
        return (
            <div>
                {{Cpnt}}
            </div>
        )
    }
}

interface HeaderProps {
    title: string
}

class Header extends React.Component <HeaderProps> {
    render () {
        const { title } = this.props
        return (
            <p>{title}</p>
        )
    }
}

I got an error on <Child Cpnt

[ts]
Type 'Element' is not assignable to type 'ComponentClass<{}, any>'.
  Type 'Element' provides no match for the signature 'new (props: {}, context?: any): Component<{}, any, any>'. [2322]

What type should I defind here?

1
  • 2
    try React.ReactElement Commented Feb 20, 2019 at 9:30

2 Answers 2

6

A React "renderable" is not React.ComponentClass but React.ReactNode.

Ask for React.ReactNode in your Cpnt prop.

Sign up to request clarification or add additional context in comments.

1 Comment

React.ReactNode worked, but got new TS error: JSX element type 'Tag' does not have any construct or call signatures.
0

You are passing <Header title='header' /> which is a React Element not Header which is a Component Class.

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.