如何编写一个 React 父组件,其中子组件位于不同的位置。

huangapple go评论71阅读模式
英文:

How to compose a react parent component where the children are placed at different locations

问题

所以我想实现一个组件,用法如下:

<Parent>
  <Child1 childName="Billy" />
  <Child2 childName="Bob" />
</Parent>

Parent 组件的实现应该是这样的:

const Parent = (props: {
    Child1: React.ReactNode;
    Child2: React.ReactNode;
  }) => {
    return (
      <>
        <div>这是标题</div>
        {props.Child1}

        <div>这是另一个组件</div>
        {props.Child2}
      </>
    );
  };

问题在于,这在 React 中无法像在 .Net Blazor 或其他支持的框架中那样工作。

我理解以下内容:

  • 我可以在 React 中使用 children 属性,但这只允许我在相同位置渲染所有子组件,而我想避免这样做。
  • 我也可以直接将各个子组件分配为 <Parent child1={<SomeChild/>} /> 的属性值。但我觉得这违背了我的代码库的美感。

总结一下,我只是想找一种替代方法来实现这个,而不是使用 prop-value assignment 或使用 children 属性。

提前感谢!

英文:

So I want to implement a component that is used like this:

<Parent>
  <Child1 childName="Billy" />
  <Child2 childName="Bob" />
</Parent>

The implementation of the Parent component should be something like this:

const Parent = (props: {
    Child1: React.ReactNode;
    Child2: React.ReactNode;
  }) => {
    return (
      <>
        <div>This is Title</div>
        {props.Child1}

        <div>This is another component</div>
        {props.Child2}
      </>
    );
  };

The problem here is, this doesn't work in react as it would in .Net Blazor or any other framework that supports it.

I understand the following:

  • I can use the children props in react but this would only allow me to render all the children at the same location which I am trying to avoid.
  • I can just go ahead and assign the various child components as property values in the form of <Parent child1={<SomeChild/>} />. But I feel this goes against the aesthetics of my code base.

Summing it all up, I just want an alternative way to implement this instead of using the prop-value assignment or using the children props.

Thanks in advance!

答案1

得分: 1

你正在将两个组件都作为 children 属性传递。你可以像这样访问它们:props.children

const Parent = ({children}: {
  children: ReactNode
}) => {
  return (
    <>
      <div>This is Title</div>
      {children[0]}

      <div>This is another component</div>
      {children[1]}
    </>
  );
};

另外,还有一个名为 Children 的对象,你可以从 React 中导入它,它包含一些用于处理子元素的方法。
https://www.smashingmagazine.com/2021/08/react-children-iteration-methods/

英文:

You are passing both components as children prop. You can access them like props.children

  const Parent = ({children}: {
    children: ReactNode
  }) =&gt; {
    return (
      &lt;&gt;
        &lt;div&gt;This is Title&lt;/div&gt;
        {children[0]}

        &lt;div&gt;This is another component&lt;/div&gt;
        {children[1]}
      &lt;/&gt;
    );
  };

Also there is an object called Children that you can import from react, that has some methods to work with children
https://www.smashingmagazine.com/2021/08/react-children-iteration-methods/

huangapple
  • 本文由 发表于 2023年4月6日 19:00:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75948741.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定