如何使用功能方法将道具传递给内部反应组件

发布于 2025-01-19 09:00:17 字数 946 浏览 2 评论 0原文

我正在使用以下功能方法进行反应

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文