【问题标题】:Creating child element in react在反应中创建子元素
【发布时间】:2021-06-08 06:20:42
【问题描述】:
<Navbar>
     <Navbar.Item><Navbar.Items>
</Navbar>

在这里我们可以轻松创建一个导航栏组件。但是如何在 react 中创建 Navbar 子元素 Navbar.Item

【问题讨论】:

标签: reactjs react-component


【解决方案1】:

这是一种称为复合组件的模式。

您可以简单地将另一个 React 组件分配为第一个的属性。

例子:

const NavBar = props => <div>....</div>;

const NavItem = props => <div>....</div>;

NavBar.Item = NavItem;

export default NavBar;

这是我第一次发现复合组件时发现的 Kent Dodds 的参考博客:https://kentcdodds.com/blog/compound-components-with-react-hooks

【讨论】:

    猜你喜欢
    • 2020-01-21
    • 2014-03-12
    • 2018-10-11
    • 2019-01-17
    • 2016-03-04
    • 2021-11-24
    • 2016-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多