Mat*_*sen 16 javascript header reactjs next.js
我正在将 GatsbyJS 项目的标头实现到 NextJS 项目中,并收到以下错误消息:
“错误:失败的道具类型:道具href需要 astring或objectin <Link>,但得到了undefined。”
从 header.jsx 中删除以下内容时,它不会提供任何错误消息: <MenuButton item={{ icon: "/images/icons/hamburger.svg" }} />
你能指导我做错了什么吗?
这是我的 header.jsx 文件:
import React, { useEffect, useRef, useState } from "react";
import styled from "styled-components";
import Link from "next/link";
import { menuData } from "../../data/menuData";
import MenuButton from "../buttons/MenuButton.jsx";
import Image from "next/image";
import MenuTooltip from "../tooltips/MenuTooltip";
export default function Header() {
const [isOpen, setIsOpen] = useState(false);
const ref = useRef();
const tooltipRef = useRef();
function handleClick(event) {
setIsOpen(!isOpen);
event.preventDefault();
}
function handleClickOutside(event) {
if (
ref.current &&
!ref.current.contains(event.target) &&
!tooltipRef.current.contains(event.target)
) {
setIsOpen(false);
}
}
useEffect(() => {
document.addEventListener("mousedown", handleClickOutside);
return () => {
document.removeEventListener("mousedown", handleClickOutside);
};
}, []);
return (
<Wrapper>
<Link href="/">
<Logo>
<Image
src="/images/logos/logo.png"
alt="Logo"
width={120}
height={100}
/>
</Logo>
</Link>
<MenuWrapper count={menuData.length} ref={ref}>
{menuData.map((item, index) => (
<MenuButton key={index} item={item} />
))}
<HamburgerWrapper onClick={(event) => handleClick(event)}>
<MenuButton item={{ icon: "/images/icons/hamburger.svg" }} />
</HamburgerWrapper>
</MenuWrapper>
<div ref={tooltipRef}>
<MenuTooltip isOpen={isOpen} />
</div>
</Wrapper>
);
}
Run Code Online (Sandbox Code Playgroud)
这是我的 menuTooltip.jsx 文件:
import React from "react";
import styled from "styled-components";
import { tooltipData } from "../../data/menuData";
import MenuButton from "../buttons/MenuButton";
export default function MenuTooltip(props) {
const { isOpen } = props;
return (
<Wrapper isOpen={isOpen}>
{tooltipData.map((item, index) => (
<MenuButton key={index} item={item} />
))}
</Wrapper>
);
}
Run Code Online (Sandbox Code Playgroud)
这是我的 MenuButton.jsx 文件:
import React from "react";
import styled from "styled-components";
import Link from "next/link";
import { Caption } from "../styles/TextStyles";
export default function MenuButton(props) {
const { item } = props;
return (
<Link href={item.link} onClick={props.onClick} key={props}>
<MenuItem hasTitle={!item.title ? false : true}>
<img src={item.icon} />
{item.title}
</MenuItem>
</Link>
);
}
Run Code Online (Sandbox Code Playgroud)
这是我的 menuData.jsx 文件:
export const menuData = [
{ title: "Learn", icon: "/images/icons/learner02_dm.svg", link: "/learn" },
{
title: "Articles",
icon: "/images/icons/article_light.svg",
link: "/articles",
},
{
title: "Community",
icon: "/images/icons/community_light.svg",
link: "/community",
},
{
title: "Entrepreneurs",
icon: "/images/icons/business02_light.svg",
link: "/entrepreneurs",
},
];
export const footerMenuData = [
{ title: "Learn", icon: "/images/icons/learner02_dm.svg", link: "/learn" },
{
title: "Articles",
icon: "/images/icons/article_light.svg",
link: "/articles",
},
{
title: "Community",
icon: "/images/icons/community_light.svg",
link: "/community",
},
{
title: "Entrepreneurs",
icon: "/images/icons/business02_light.svg",
link: "/entrepreneurs",
},
{
title: "Updates",
icon: "/images/icons/calendar_light.svg",
link: "/updates",
},
{
title: "Help",
icon: "/images/icons/help_light.svg",
link: "/help",
},
];
export const tooltipData = [
{ title: "Learn", icon: "/images/icons/learner02_dm.svg", link: "/learn" },
{
title: "Articles",
icon: "/images/icons/article_light.svg",
link: "/articles",
},
{
title: "Community",
icon: "/images/icons/community_light.svg",
link: "/community",
},
{
title: "Entrepreneurs",
icon: "/images/icons/business02_light.svg",
link: "/entrepreneurs",
},
];
Run Code Online (Sandbox Code Playgroud)
Sha*_*mul 14
哦,天哪!我被这个问题困扰了很长时间,最后终于解决了。虽然我很高兴接受的答案对你有用,但遗憾的是它不适合我。因此,我将在NextJS 官方 GitHub 存储库的讨论线程中找到的修复程序保留在这里。
而不是将链接密钥直接传递到Linkshref属性中:
<Link href={item.link} {...other props}>...</Link>
Run Code Online (Sandbox Code Playgroud)
以字符串形式传递它,如下所示:
<Link href={`${item.link}`} {...other props}>...</Link>
Run Code Online (Sandbox Code Playgroud)
如果需要,您可以将组件的子Link组件包装在<a>标签中,但这不是必需的,并且我的组件无需这样做即可工作。仅供参考,我在组件内有一些 SVG 图标Link,这些图标造成了所有这些麻烦。叹!
小智 6
您在链接中添加一个标签 a ,如下所示:
<link href....>
<a>
<image>
<p> text </p>
</a>
</link>
Run Code Online (Sandbox Code Playgroud)
我也经常遇到这个错误...
对于从 React-router-dom 迁移到 next.js 内置 Link 组件的其他人。
\n这个错误发生在我身上,因为我正在从react-router-dom迁移到Next.js中的内置Link组件,而我忘记将组件to=""中的属性<Link></Link>href=""
错误的\xe2\x9d\x8c
\n<Link to=""></Link>\nRun Code Online (Sandbox Code Playgroud)\n正确\xe2\x9c\x85
\n<Link href=""></Link>\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
31618 次 |
| 最近记录: |