【发布时间】:2021-07-11 17:50:57
【问题描述】:
ReactJs 官方文档建议在 dot notation 之后创建组件,例如 React-bootstrap 库:
<Card>
<Card.Body>
<Card.Title>Card Title</Card.Title>
<Card.Text>
Some quick example text to build on the card title and make up the bulk of
the card's content.
</Card.Text>
</Card.Body>
</Card>
感谢this question,我知道我可以像在javascript中那样使用函数式组件来创建这个结构:
const Card = ({ children }) => <>{children}</>
const Body = () => <>Body</>
Card.Body = Body
export default Card
使用 TypeScript 我决定为其添加相应的类型:
const Card: React.FunctionComponent = ({ children }): JSX.Element => <>{children}</>
const Body: React.FunctionComponent = (): JSX.Element => <>Body</>
Card.Body = Body // <- Error: Property 'Body' does not exist on type 'FunctionComponent<{}>'
export default Card
现在的问题是 TypeScript 不允许分配 Card.Body = Body 并给我错误:
“FunctionComponent”类型上不存在属性“Body”
那么我怎样才能正确输入这个才能使用这个代码结构呢?
【问题讨论】:
标签: javascript reactjs typescript