如何使用功能方法将道具传递给内部反应组件
我正在使用以下功能方法进行反应
const Divider: React.FunctionComponent = ({children}) => (
<div>
// I want to use color props here of Card
divider
{children}
</div>
);
const Card: React.FunctionComponent = ({children, color}) => {
const color = props.color
return(
<div>
card
{children}
</div>
)}
Card.Divider = Divider;
,并将这些组件称为以下
<Card color="red">
<Card.Divider>
Any text
<Card.Divider/>
<Card />
的问题:我如何在分隔器组件中获取颜色道具
ps:i 不要想要重复再次通过划分的道具,例如遵循
<Card color="red">
<Card.Divider color="red">
Any text
<Card.Divider/>
<Card />
我想要的是以下方法,并且仍然能够在分隔组件中使用颜色道具,我该怎么做
<Card color="red">
<Card.Divider>
Any text
<Card.Divider/>
<Card />
I am using react with the following functional approach
const Divider: React.FunctionComponent = ({children}) => (
<div>
// I want to use color props here of Card
divider
{children}
</div>
);
const Card: React.FunctionComponent = ({children, color}) => {
const color = props.color
return(
<div>
card
{children}
</div>
)}
Card.Divider = Divider;
and I call these components as following
<Card color="red">
<Card.Divider>
Any text
<Card.Divider/>
<Card />
The problem is: How can I get the color props in the Divider component
PS: I don't want to repeat props passing again for divider like following
<Card color="red">
<Card.Divider color="red">
Any text
<Card.Divider/>
<Card />
What I want is the following approach and can still able to use color props in Divider component, how can I do that
<Card color="red">
<Card.Divider>
Any text
<Card.Divider/>
<Card />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论