profile
viewpoint

Ask questionsString is not a valid ComponentType

Hello I creating a component that takes a component to render as props. However, it's being impossible for me to type that it can accept both react components and default components (those defined by strings). Flow says that string is not a valid type for ComponentType

// @flow
import * as React from "react";


type Props = {
  component: React.ComponentType<*>
}
  
const Bla = ({component: C }: Props) => <C/>

Bla.defaultProps = {
  component: 'div'
}

const x = <Bla/>

Flow version: 0.125.1

Expected behavior

I expect string to be a valid component

Actual behavior

Only react components can be used

  • Link to Try-Flow or Github repo: https://flow.org/try/#0PTAEAEDMBsHsHcBQBLAtgB1gJwC6gFSgCGAzqAEoCmRAxnpFrKqAERbV0sDcivOAnukqgACo3RkAvKADeiUKBpNMAO0oqcALgoccAOgDCy2Go0AVQZQA8+AHyIAvvNCIlKkngBC0IqGkAKGSUME3UtUANQB20xWAkASj9bUCsDYHtEbyI9ABNKSCIAV2gcWIk-WWdg1TDtAHIc5AA3OsdeNw9QAA8Kqyz0oA
facebook/flow

Answer questions dsainati1

What you are looking for is React.ElementType which is the type of all things that can be inputs to a React element (strings and components).

https://github.com/facebook/flow/blob/master/lib/react.js#L170

useful!

Related questions

Common spread pattern leads to "exponentially large number of cases" errors hot 1
Flow issue "Please upgrade flow to version >=0.75." hot 1
Object.fromEntries type defs missing hot 1
Cannot set defaultProps with React.memo and React.forwardRef hot 1
Inexact rest object claims it may have properties it can't possibly have hot 1
Inexact rest object claims it may have properties it can't possibly have hot 1
Inexact rest object claims it may have properties it can't possibly have hot 1
0.92.0 release crashes on startup (Unix.Unix_error) hot 1
module.name_mapper does not find nested modules on windows hot 1
Cannot import useState because there is no useState export in react. hot 1
Can't use a default value from a destructuring assignment as a computed property since 0.111 hot 1
Flow issue "Please upgrade flow to version >=0.75." hot 1
Allow explicitly including a folder under an ignored path hot 1
Flow caches and fails to purge errors hot 1
Version `0.105.0` is missing the binaries hot 1
Github User Rank List