简体   繁体   English

蚂蚁设计:将菜单项包装到自定义组件中

[英]Ant design: Wrapping a menu item into a custom component

I'm playing around with ant-design and trying to structure a simple menu, and everything works as expected: 我正在研究蚂蚁设计,并试图构建一个简单的菜单,并且一切都按预期工作:

<Menu mode="inline">
  <Menu.Item key="/">
    <Icon type="dashboard" theme="outlined" />
    Dashboard
  </Menu.Item>
  <Menu.Item key="/transactions">
    <Icon type="bars" theme="outlined" />
    Transactions
  </Menu.Item>
  <Menu.Item key="/groups">
    <Icon type="team" theme="outlined" />
    Groups
  </Menu.Item>
  <Menu.Item key="/account">
    <Icon type="idcard" theme="outlined" />
    Account
  </Menu.Item>
</Menu>

( https://codesandbox.io/s/wqn37ojmv7 ) https://codesandbox.io/s/wqn37ojmv7

Now, I wanted to DRY up this code a bit, by making a separate wrapped MenuItem component: 现在,我想通过制作一个单独的包装MenuItem组件来稍微干燥一下此代码:

const MenuItem = ({route, icon, children}) => (
  <Menu.Item key={route}>
    <Icon type={icon} theme="outlined" />
    {children}
  </Menu.Item>
);

// ...
<Menu mode="inline">
  <MenuItem route="/" icon="dashboard">Dashboard</MenuItem>
  <MenuItem route="/transactions" icon="bars">Transactions</MenuItem>
  <MenuItem route="/groups" icon="team">Groups</MenuItem>
  <MenuItem route="/account" icon="idcard">Account</MenuItem>
</Menu>

However, substituting my shiny new component will pretty much break everything - somehow I seem to lose some props that were magically passed to the Menu.Item s before (like a clsPrefix or a onItemHover -callback): https://codesandbox.io/s/ojyqy0oqq6 但是,替换我闪亮的新组件几乎会破坏一切-我似乎以某种方式丢失了之前魔术传递给Menu.Item的一些道具(例如clsPrefixonItemHover ): https : onItemHover S / ojyqy0oqq6

What is going on here? 这里发生了什么? How are these props passed behind the scenes and how can I wrap Menu.Item correctly without losing all of this magic? 这些道具如何在幕后传递,如何在不丢失所有魔术的情况下正确包装Menu.Item

You could pass the rest of the passed props 您可以传递其余传递的道具

const MenuItem = ({route, icon, children, ...props}) => ( 
    <Menu.Item key={route} {...props}> 
        <Icon type={icon} theme="outlined" />
        {children}
    </Menu.Item> );

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

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