简体   繁体   English

如何在 React 和 Typescript 中将组件作为道具传递?

[英]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.

相关问题 如何在 React 中将 const 变量作为道具传递给组件? - How do I pass a const variable to a component as a prop in React? 如何将 Prop 传递给导航屏幕组件 - React Native - How do I pass a Prop to a Navigation Screen Component - React Native 如何在父项更改时将道具传递给反应组件但不更新子项中的该道具? - How do I pass a prop to a react component yet not update that prop in the child when parent changes? 如何将React组件作为prop传递 - How to pass a React component as a prop 如何将组件作为道具传递给 React 中的另一个组件? - How can I pass a component as a prop into another component in React? 如何仅在具有 Typescript 的容器组件的上下文中使用 React 道具? - How do I make a React prop only required when used in the context of a Container component with Typescript? 如何为带有数据数组的 React 组件传递内联样式的道具? - How do I pass a prop for inline styling for React component with an array of data? 如何将 promise 中的值传递给 react native 的组件 prop? - How do I pass a value from a promise to a component prop in react native? React Reusable Button Component - 如何正确传递背景颜色道具? - React Reusable Button Component - How do I properly pass a background color prop? 如何将 map 操作作为道具传递给组件(反应日期选择器)? - How can I pass a map operation to a component (react datepicker) as a prop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM