简体   繁体   English

如何在 React mui menuItem 中传递组件

[英]How to pass component in React mui menuItem

I imported Report.js component and want to use it in "menuitem", I tried this in button and its working perfectly我导入了 Report.js 组件并想在“menuitem”中使用它,我在按钮中尝试了这个并且它工作得很好

 import Reports from 'new-components/Reports/Reports'   //ontop

 <Button> 
    <Reports pid={pid} />          //working
 </Button>

but when i tried this same in "menuitem" than its not working但是当我在“menuitem”中尝试同样的方法时,它不起作用

<MenuItem >
  <Reports pid={pid} />            //not working
</MenuItem>

i also tried with passing component in onClick function, i know we can only pass function in onClick, but this gives desired result with error我也尝试在 onClick 函数中传递组件,我知道我们只能在 onClick 中传递函数,但这会给出所需的结果但有错误

<MenuItem onClick={<Reports pid={name}/>}> 
     <Reports pid={name}/>
</MenuItem>

It throws a error " Uncaught Error: Expected onClick listener to be a function, instead got a value of object type. "它抛出一个错误“未捕获的错误:预期onClick侦听器是一个函数,而不是得到一个object类型的值。

Reports.js报告.js

 return ( <CSVLink {...csvReport}>Export CSV</CSVLink> )

You could try to pass your Reports element via the component prop of MenuItem :您可以尝试通过MenuItemcomponent属性传递您的Reports元素:

const YourComponent = () => {
  return <MenuItem component={<Reports pid={pid} />} />;
}

https://mui.com/material-ui/api/menu-item/#props https://mui.com/material-ui/api/menu-item/#props

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

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