Bruno Quaresma
Bruno Quaresma

Reputation: 10687

Flow - Render component passed by props

I'm trying to using flow in a Button React component.

// @flow

import * as React from 'react'

type Props = {
  color: string,
  block: boolean,
  component: React.Component<*> | string,
  className: string,
  domProps: Object
}

const Button = ({
  color,
  block,
  component,
  className,
  ...domProps
}: Props) => {
  const Component = component

  return (
    <Component
      {...domProps}
      className={`
        ${className}
        btn
        btn--${color}
        ${block ? 'btn--block' : ''}
      `}
    />
  )
}

Button.defaultProps = {
  component: 'button',
  block: false
}

export default Button

But when I try to render the custom Component flow is displaying this error:

[flow] Cannot create Component element because React.Component [1] is not a React component. (References: [1])

How can I do this work?

Upvotes: 1

Views: 539

Answers (1)

richardgirges
richardgirges

Reputation: 1532

You can't use React.Component as a type definition in Flow. There are built-in type definitions in Flow for React. Here's what you're looking for:

React.ComponentType

Example usage:

type Props = {
  color: string,
  block: boolean,
  component: React.ComponentType<*> | string,
  className: string,
  domProps: Object
}

Read all about other React type definitions here: https://flow.org/en/docs/react/types/

Upvotes: 2

Related Questions