javascript - 如何使用材料 ui 中的“样式”反应
问题描述
我正在尝试导出样式化的 AppBar,这是我的代码
import * as React from 'react';
import { styled, useTheme } from '@mui/material/styles';
import MuiAppBar from '@mui/material/AppBar';
import Toolbar from '@mui/material/Toolbar';
import Typography from '@mui/material/Typography';
import MenuIcon from '@mui/icons-material/Menu';
const MuiAppBar = styled(MuiAppBar, {
shouldForwardProp: (prop) => prop !== 'open',
})(({ theme, open }) => ({
zIndex: theme.zIndex.drawer + 1,
transition: theme.transitions.create(['width', 'margin'], {
easing: theme.transitions.easing.sharp,
duration: theme.transitions.duration.leavingScreen,
}),
...(open && {
marginLeft: drawerWidth,
width: `calc(100% - ${drawerWidth}px)`,
transition: theme.transitions.create(['width', 'margin'], {
easing: theme.transitions.easing.sharp,
duration: theme.transitions.duration.enteringScreen,
}),
}),
}));
const AppBar = () => {
return (
<div>
<MuiAppBar position="fixed" open={open}>
<Toolbar>
<IconButton
color="inherit"
aria-label="open drawer"
onClick={handleDrawerOpen}
edge="start"
sx={{
marginRight: '36px',
...(open && { display: 'none' }),
}}
>
<MenuIcon />
</IconButton>
<Typography variant="h6" noWrap component="div">
Mini variant drawer
</Typography>
</Toolbar>
</MuiAppBar>
</div>
)
}
export default AppBar
这给出了预先声明的错误
Line 12:7: Parsing error: Identifier 'MuiAppBar' has already been declared.
解决方案
您不能导入:
import MuiAppBar from '@mui/material/AppBar';
然后创建一个同名的函数表达式。只需将函数表达式的名称更改为其他名称,然后在 jsx 中使用该名称
例子:
const CustomMuiAppBar = .....
///
<CustomMuiAppBar />
推荐阅读
- eclipse - eclipse中的程序执行太慢了,昨天同一程序的执行速度很快
- c# - 如何调用,API 中的 POST 方法,其中包含一个类 Content
- spring - How to config JPA in spring mvc?
- pentaho - 为什么我在 pentaho cde 中传递日期参数时看不到示例数据图表?
- reactjs - reactjs链接不起作用,在ajax请求后渲染时
- netsuite - 通过 Suitelet 使用 Netsuite 信用卡付款
- primefaces - 在 Primefaces 数据表单元格编辑器中选择每个项目上的选择行复选框
- python - 为衰减变量优化以下python代码部分
- git - 如何在 azure devops 中设置默认分支策略
- c - 试图从 C 代码中获取 windows 版本