reactjs - 使用带有 Drawer 组件的 Material-UI Box 组件
问题描述
Material-UI Box 组件允许我们引用其他组件,如下所示:
import Button from "@material-ui/core/Button";
import Box from "@material-ui/core/Box";
const NewButton = ({ children }) => (
<Box compoment={Button} px={3} py={1} color="white" bgcolor="primary.dark">
{children}
</Box>
)
这就像我想要的那样工作。但是,现在让我尝试使用 Drawer 组件:
import Drawer from "@material-ui/core/Drawer";
import Box from "@material-ui/core/Box";
const NewDrawer = ({ children, open }) => {
return (
<Box component={Drawer} width="300px" bgcolor="secondary.dark">
{children}
</Box>
)
}
这不起作用。
知道为什么不以及如何让它工作吗?
谢谢。
解决方案
根据 Material UI 文档,对于Drawer组件,我们必须将open
prop 传递为true
.
并且还需要像下面这样传递抽屉内容,
<Drawer open={true}>{renderContents()}</Drawer>
在 Box 组件api中,我们可以将组件数据作为“函数”传递。像下面的例子。
import Drawer from "@material-ui/core/Drawer";
import Box from "@material-ui/core/Box";
const NewDrawer = ({ children, open }) => {
return (
<Box component={() => {
return <Drawer open={true}>{renderContents()}</Drawer>
}} width="300px" bgcolor="secondary.dark">
{children}
</Box>
)
}
请参阅我的代码沙箱示例。
推荐阅读
- ruby-on-rails - 在构建 Rails Gem 时,如何让 Rails 在查找视图部分时加载的 ActionView 路径中包含我的 gem 的 app/views/?
- python - 对于大小为 0 的轴 0,索引超出范围
- jquery - 向序列化的表单数据添加额外的数组
- java - MyBatis Spring Boot 分页查询在 Oracle 11g 中很慢
- laravel - VueJs 组件未显示在页面中(使用 Laravel 框架)
- wpf - 如何将 WPF 应用程序集成到 Unity3D 项目中?
- javascript - ReactJS:使用 onClick 和 state 来显示元素的子集
- json - VB.NET HTTP Json 响应解码
- mongodb - 如何将 android 设备而不是 AVD 与数据库连接
- c# - csharp中emgucv的全局阈值