简体   繁体   English

反应 Typescript 子组件

[英]React Typescript subcomponent

I'm trying to make a React Component with Subcomponents in the style of: https://react-bootstrap.github.io/components/cards/ which should render one component on the left and one on the right我正在尝试使用以下样式的子组件制作 React 组件: https://react-bootstrap.github.io/components/cards/它应该在左侧呈现一个组件,在右侧呈现一个组件

<MyComponent>
   <MyComponent.Left>
      foo
   </MyComponent.Left>
   <MyComponent.Right>
      bar
   </MyComponent.Right>
</MyComponent>

My basic strategy has been to create something like this:我的基本策略是创建这样的东西:

function MyComponent(props:PropsWithChildren):JSX.Element{
var leftComponent = ???;
var rightComponent = ???;
return 
(<div>
   <div className="this-goes-on-the-right">leftComponent</div>
   <div className="this-goes-on-the-left">rightComponent</div>
</div>);
}

function MyComponent.Left = function MyComponentLeft(props:PropsWithChildren){
   return (<div>props.children</div>);
}
function MyComponent.Right = function MyComponentRight(props:PropsWithChildren){
   return (<div>props.children</div>);
}

But I'm lost as to how to figure out which of the children passed to MyComponent is the MyComponent.Left and which is the MyComponent.Right.但是我不知道如何确定传递给 MyComponent 的哪些子项是 MyComponent.Left,哪些是 MyComponent.Right。 How can I do this in typescript?我如何在 typescript 中执行此操作?

They are using Object.assign to assign "sub components".他们正在使用Object.assign来分配“子组件”。

const Card: BsPrefixRefForwardingComponent<'div', CardProps> = React.forwardRef<
  HTMLElement,
  CardProps
>(
  (
    {
      props
    },
    ref,
  ) => {
    return (
      <Component
        ...
      </Component>
    );
  },
);

Card.displayName = 'Card';
Card.propTypes = propTypes;
Card.defaultProps = defaultProps;

export default Object.assign(Card, {
  Img: CardImg,
  Title: CardTitle,
  Subtitle: CardSubtitle,
  Body: CardBody,
  Link: CardLink,
  Text: CardText,
  Header: CardHeader,
  Footer: CardFooter,
  ImgOverlay: CardImgOverlay,
});

Source 来源

  1. There are multiple ways probably to approach what you are aiming at.可能有多种方法可以达到您的目标。 The most basic could be to set some names to the components and check each child for it's name ( react docs ) but I would not recommend this.最基本的可能是为组件设置一些名称并检查每个子组件的名称( react docs ),但我不推荐这样做。

  2. Rather you should style your MyComponent.Left and MyComponent.Right properly so they are shown the desired way no matter of the order they are passed to the MyComponent 's children.相反,您应该正确设置MyComponent.LeftMyComponent.Right的样式,以便无论它们传递给MyComponent的子级的顺序如何,它们都以所需的方式显示。

Sketch up of what I mean:勾勒出我的意思:

function MyComponent(props:PropsWithChildren):JSX.Element{
  return (
    <div>{props.children}</div>
  );
}

function MyComponent.Left = function MyComponentLeft(props:PropsWithChildren){
   return (<div className="this-goes-on-the-left">props.children</div>);
}
function MyComponent.Right = function MyComponentRight(props:PropsWithChildren){
   return (<div className="this-goes-on-the-right">props.children</div>);
}

The implementation of the classes which style the nested component could be based on flex-box rules, or floating or whatever suits your use case.嵌套组件样式的类的实现可以基于 flex-box 规则、浮动规则或任何适合您的用例的规则。

  1. One more option which is slightly off your example but might be useful is to pass the components as props, not children like:与您的示例略有不同但可能有用的另一种选择是将组件作为道具传递,而不是孩子喜欢:
function MyComponent(props:PropsWithChildren):JSX.Element{
  return (
    <div>
       <div className="this-goes-on-the-right">{props.right}</div>
       <div className="this-goes-on-the-left">{props.left}</div>
    </div>
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM