简体   繁体   中英

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?

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM