Next.js -为什么我得到水合错误在我的头组件



我正在一个作业门户上工作,我使用JWT获得用户身份验证并将其存储在本地存储中。我得到水合错误在我的头组件时,身份验证,如果我重新加载我的网页。我不知道如何解决这个问题,因为我是新的next.js。

Header.js

import React from "react";
import { useState, useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";
import Link from "next/link";
import Image from "next/image";
import { useRouter } from "next/router";
import Navbar from "react-bootstrap/Navbar";
import Container from "react-bootstrap/Container";
import Nav from "react-bootstrap/Nav";
import Dropdown from "react-bootstrap/Dropdown";
import Button from "react-bootstrap/Button";
import { logout } from "../actions/userActions";
const Header = ({ acclink, acc }) => {
const userLogin = useSelector((state) => state.userLogin);
const { userInfo } = userLogin;
const dispatch = useDispatch();
const logoutHandler = () => {
dispatch(logout());
};
return (
<>
{/* Dark navbar: Light links against dark background */}
<Navbar
as="header"
className="mb-5 navbar-stuck"
bg="dark"
variant="dark"
fixed="top"
expand="lg"
>
<Container>
{/* Logo (Brand) */}
<Link href="/" passHref>
<Navbar.Brand className="me-2 me-xl-4">
<Image
src="/images/Apt-Job-logo.svg"
width={116}
height={32}
quality={100}
alt="Apt Job"
/>
</Navbar.Brand>
</Link>
{/* Mobile menu toggle */}
<Navbar.Toggle
aria-controls="navbarNav"
bsPrefix="ms-auto navbar-toggler"
/>
{/* Action button */}
{userInfo ? (
<Nav.Item
as={Dropdown}
bsPrefix="d-none d-lg-block order-lg-3 my-n2 me-3"
>
<Dropdown.Toggle
as={Nav.Link}
className="dropdown-toggle-flush d-flex py-1 px-0"
>
<Image
alt="User"
src="/images/user.png"
width={40}
height={40}
quality={100}
/>
</Dropdown.Toggle>
<Dropdown.Menu align="end" renderOnMount variant="dark">
<Dropdown.Item className="d-flex align-items-start border-bottom border-light px-3 py-1 mb-2">
<div className="ps-2">
<h6 className="fs-base mb-0 text-light">{userInfo.name}</h6>
<div className="fs-xs py-2">{userInfo.email}</div>
</div>
</Dropdown.Item>
{userInfo.isEmployer ? (
<Link href="#" passHref>
<Dropdown.Item>Action link</Dropdown.Item>
</Link>
) : (
<Link href="/user/profile" passHref>
<Dropdown.Item>Profile Settings</Dropdown.Item>
</Link>
)}
{userInfo.isEmployer ? (
<Link href="#" passHref>
<Dropdown.Item>Employer action</Dropdown.Item>
</Link>
) : (
<Link href="#" passHref>
<Dropdown.Item>Another action</Dropdown.Item>
</Link>
)}
<Dropdown.Divider as="div" />
<Dropdown.Item onClick={logoutHandler}>Signout</Dropdown.Item>
</Dropdown.Menu>
</Nav.Item>
) : (
(acc == "usr" || acc == "emp") && (
<Link href={`${acclink}/login`} passHref>
<Button
variant="link"
size="sm"
className="btn-light d-none d-lg-block order-lg-3"
// onClick={() => router.push("/login")}
>
<i className="fi-user me-2"></i>
<span className="d-none d-sm-inline">Sign in</span>
</Button>
</Link>
)
)}
{acc == "usr" && (
<Link href="/postresume" passHref>
<Button
variant="primary"
size="sm"
className=" rounded-pill order-lg-3 ms-2"
>
<i className="fi-plus me-2"></i>
Post<span className="d-none d-sm-inline"> Resume</span>
</Button>
</Link>
)}
{!userInfo && acc == "" && (
<>
<Link href="/user" passHref>
<Button
variant="link"
size="sm"
className="btn-light d-none d-lg-block order-lg-3"
>
<span className="d-none d-sm-inline">For Users </span>
<i className="fi-arrow-long-right ms-2"></i>
</Button>
</Link>
<Link href="/employer" passHref>
<Button
variant="link"
size="sm"
className="btn-light d-none d-lg-block order-lg-3"
>
<span className="d-none d-sm-inline">For Employers </span>
<i className="fi-arrow-long-right ms-2"></i>
</Button>
</Link>
</>
)}
{/* Responsive Navbar nav (Menu) */}
<Navbar.Collapse id="navbarNav" className="order-md-2">
<Nav className="navbar-nav-scroll" style={{ maxHeight: "35rem" }}>
<Nav.Item>
<Link href="/user" passHref>
<Nav.Link>Find Jobs</Nav.Link>
</Link>
</Nav.Item>
{userInfo && userInfo.isEmployer ? (
<Nav.Item>
<Link href="#" passHref>
<Nav.Link> Post Jobs</Nav.Link>
</Link>
</Nav.Item>
) : (
userInfo &&
userInfo.isEmployer == false && (
<Nav.Item>
<Link href="#" passHref>
<Nav.Link>Applied Jobs</Nav.Link>
</Link>
</Nav.Item>
)
)}
{(!userInfo && acc == "usr") ||
(acc == "emp" && (
<Nav.Item className="d-lg-none">
<Link href={`${acclink}/login`} passHref>
<Nav.Link>
<i className="fi-user me-2"></i>
{"Sign in"}
</Nav.Link>
</Link>
</Nav.Item>
))}

</Nav>
</Navbar.Collapse>
</Container>
</Navbar>
</>
);
};

export default Header;

我确信水合错误是因为标题,我从页面中删除了标题组件,并在经过身份验证时进行了测试,当我在经过身份验证并重新加载页面时,它显示没有水合错误。

如果组件中有语法或HTML语义错误,也会导致水合错误。

我的是由于嵌套按钮。

相关内容

  • 没有找到相关文章

最新更新