[英]How do I pass a component as a prop in React and Typescript?
I am new to React and am building a tab component using Material UI's Tabs component.我是 React 新手,正在使用 Material UI 的 Tabs 组件构建一个选项卡组件。 I'd like to place the Material UI badge component within the Tab component's label prop, but I'm not sure how to go about this.
我想将 Material UI 徽章组件放置在 Tab 组件的 label 道具中,但我不确定如何 go 来解决这个问题。
The Tab component looks as such: Tab 组件如下所示:
<Tab
key={i}
label={label}
{...globalTabProps}
{...tabProps}
classes={{
wrapper: cx('MuiTab-wrapper'),
}}
/>
I'm trying to add the badge as such:我正在尝试这样添加徽章:
const label = {
<Badge
color="primary"
className={
badgeProps.badgeContent === ''
? classNames(classes.MuiBadge, classes.MuiBadgeDotted)
: classNames(classes.MuiBadge, classes.MuiBadgeNumber)
}
badgeContent={''}
invisible={false}
{...globalBadgeProps}
{...badgeProps}
></Badge>
};
Of course, this errors out (parsing error), but I don't think this is the correct way to handle this anyway.当然,这个错误(解析错误),但我不认为这是处理这个问题的正确方法。
Would anyone be able to point me in the right direction?有人能指出我正确的方向吗?
Many thanks!非常感谢!
You should wrap it with ()
, like so.你应该用
()
包装它,就像这样。
const label = (
<Badge
color="primary"
className={
badgeProps.badgeContent === ''
? classNames(classes.MuiBadge, classes.MuiBadgeDotted)
: classNames(classes.MuiBadge, classes.MuiBadgeNumber)
}
badgeContent={''}
invisible={false}
{...globalBadgeProps}
{...badgeProps}
></Badge>
)
Note the ()
wrapping it.注意
()
包装它。
Then do it like so:然后这样做:
<Tab
key={i}
label={label}
{...globalTabProps}
{...tabProps}
classes={{
wrapper: cx('MuiTab-wrapper'),
}}
/>
What it is done inside:它在里面做了什么:
const WhateverComponent = (props) => (
<div>
...
{props.label}
</div>
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.