如何将另一个组件附加到ComponentType?

问题描述 投票:0回答:1

有没有办法将另一个组件附加到已经作为ComponentType键入的组件?

E.g:

type Props = {
  name: string,
}

const Header: ComponentType<Props> = ({ props }) => (<h1>{name}</h1>)
const SubHeader: ComponentType<Props> = ({ props }) => (<h2>{name}</h2>)

Header.SubHeader = SubHeader // this is causes a flow error due to ComponentType

这个例子有点人为,但是当使用HOC或ComponentType时,有时需要手动强制使用styled-components

reactjs flowtype
1个回答
0
投票

交叉路口将完成这项工作:const Header: ComponentType<Props> & { SubHeader: ComponentType<Props> }

请注意,SubHeader不能是Maybe或可选类型,因为如果您尝试将Maybe或可选类型作为组件呈现,Flow会抱怨。

© www.soinside.com 2019 - 2024. All rights reserved.