gpt4 book ai didi

javascript - 警告 : Function components cannot be given refs

转载 作者:行者123 更新时间:2023-12-05 00:24:18 31 4
gpt4 key购买 nike

我正在使用 Next.js 最新版本来制作我的博客网站,不知道为什么显示错误,当我尝试制作我的表单时,然后显示如下错误:

Warning: Function components cannot be given refs. 
Attempts to access this ref will fail. Did you mean to use React.forwardRef()?
enter image description here
我试过下面的代码:
import React, { useState } from "react";
import { APP_NAME } from "../config";
import Link from "next/link";
import {
Collapse,
Navbar,
NavbarToggler,
NavbarBrand,
Nav,
NavItem,
NavLink,
} from "reactstrap";

const Header = () => {
const [isOpen, setIsOpen] = useState(false);
const toggle = () => setIsOpen(!isOpen);

return (
<div>
<Navbar color="light" light expand="md">
<Link href="/">
<NavbarBrand className="font-weight-bold">{APP_NAME}</NavbarBrand>
</Link>
<NavbarToggler onClick={toggle} />
<Collapse isOpen={isOpen} navbar>
<Nav className="m-auto" navbar>
<NavItem>
<Link href="/signup">
<NavLink style={{ cursor: "pointer" }}>Signup</NavLink>
</Link>
</NavItem>
<NavItem>
<Link href="/signin">
<NavLink style={{ cursor: "pointer" }}>Signin</NavLink>
</Link>
</NavItem>
</Nav>
</Collapse>
</Navbar>
</div>
);
};

export default Header;


请给我您的宝贵建议。

最佳答案

最简单的解决方案可能是用原生 html 元素包装您的 NavLink。在你的情况下:

<Link href="/signup" passHref>
<a>
<NavLink style={{ cursor: "pointer" }}>Signup</NavLink>
</a>
</Link>

关于javascript - 警告 : Function components cannot be given refs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66421459/

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