gpt4 book ai didi

reactjs - Logo 未显示在 AppBar 中(Material UI/React)

转载 作者:行者123 更新时间:2023-12-04 03:40:11 24 4
gpt4 key购买 nike

由于某种原因, Logo 没有显示在我的 AppBar 上。这是我得到的:

Screenshot of the current AppBar

这是我的代码:

import {
AppBar,
Toolbar,
Typography,
makeStyles,
Button,
IconButton,
Drawer,
Link,
MenuItem,
} from "@material-ui/core";
import MenuIcon from "@material-ui/icons/Menu";
import React, { useState, useEffect } from "react";
import { Link as RouterLink } from "react-router-dom";
import logo from '../overland-ninja-logo.png';

const headersData = [
{
label: "Listings",
href: "/listings",
},
{
label: "Mentors",
href: "/mentors",
},
{
label: "My Account",
href: "/account",
},
{
label: "Log Out",
href: "/logout",
},
];

const useStyles = makeStyles(() => ({
header: {
backgroundColor: "#400CCC",
paddingRight: "79px",
paddingLeft: "118px",
"@media (max-width: 900px)": {
paddingLeft: 0,
},
},
logo: {
fontFamily: "Work Sans, sans-serif",
fontWeight: 600,
color: "#FFFEFE",
textAlign: "left",
},
menuButton: {
fontFamily: "Open Sans, sans-serif",
fontWeight: 700,
size: "18px",
marginLeft: "38px",
},
toolbar: {
display: "flex",
justifyContent: "space-between",
},
drawerContainer: {
padding: "20px 30px",
},
}));

export default function Header() {
const { header, logo, menuButton, toolbar, drawerContainer } = useStyles();

const [state, setState] = useState({
mobileView: false,
drawerOpen: false,
});

const { mobileView, drawerOpen } = state;

useEffect(() => {
const setResponsiveness = () => {
return window.innerWidth < 900
? setState((prevState) => ({ ...prevState, mobileView: true }))
: setState((prevState) => ({ ...prevState, mobileView: false }));
};

setResponsiveness();

window.addEventListener("resize", () => setResponsiveness());
}, []);

const displayDesktop = () => {
return (
<Toolbar className={toolbar}>
{femmecubatorLogo}
<div>{getMenuButtons()}</div>
</Toolbar>
);
};

const displayMobile = () => {
const handleDrawerOpen = () =>
setState((prevState) => ({ ...prevState, drawerOpen: true }));
const handleDrawerClose = () =>
setState((prevState) => ({ ...prevState, drawerOpen: false }));

return (
<Toolbar>
<IconButton
{...{
edge: "start",
color: "inherit",
"aria-label": "menu",
"aria-haspopup": "true",
onClick: handleDrawerOpen,
}}
>
<MenuIcon />
</IconButton>

<Drawer
{...{
anchor: "left",
open: drawerOpen,
onClose: handleDrawerClose,
}}
>
<div className={drawerContainer}>{getDrawerChoices()}</div>
</Drawer>

<div>{femmecubatorLogo}</div>
</Toolbar>
);
};

const getDrawerChoices = () => {
return headersData.map(({ label, href }) => {
return (
<Link
{...{
component: RouterLink,
to: href,
color: "inherit",
style: { textDecoration: "none" },
key: label,
}}
>
<MenuItem>{label}</MenuItem>
</Link>
);
});
};

const femmecubatorLogo = (
<img src={logo} />
);

const getMenuButtons = () => {
return headersData.map(({ label, href }) => {
return (
<Button
{...{
key: label,
color: "inherit",
to: href,
component: RouterLink,
className: menuButton,
}}
>
{label}
</Button>
);
});
};

return (
<header>
<AppBar className={header}>
{mobileView ? displayMobile() : displayDesktop()}
</AppBar>
</header>
);
}

我对 react 很陌生,我对如何去做这件事感到有点困惑。我已经尝试了所有教程,但找不到正确的解决方案。任何帮助将不胜感激。

最佳答案

您正在导入图像后重新分配 logo 的值。

import logo from "../overland-ninja-logo.png"; // value of logo is correctly assigned to a path to the image
...
const useStyles = makeStyles(() => ({
logo: {
fontFamily: "Work Sans, sans-serif",
fontWeight: 600,
color: "#FFFEFE",
textAlign: "left"
},
}));
...
const { header, logo, menuButton, toolbar, drawerContainer } = useStyles(); // value of logo is incorrectly assigned to a an object

如果您使用的是 vscode,您应该在 import logo 处收到 typescript 警告:

'logo' is declared but its value is never read.ts(6133)

关于reactjs - Logo 未显示在 AppBar 中(Material UI/React),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66191774/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com