我刚刚开始了nextJS 13.4.12的工作,目前正在将一个项目从Reaction转移到NextJS.在处理登录页面时,我发现nextJS不允许在服务器端组件上使用react-hook-form
或@hookform/resolvers/yup
.要使它们正常工作,您需要在组件的最顶端添加use client
.我也这么做了.
但令我惊讶的是,当我看到网络请求时,我仍然可以看到基本的HTML代码,其中包括输入、按钮和其他一些标记. SSR是如何在这里发生的?
PS: I have seen a ton of articles, tutorials, snippets, and docs. They were directly using hook-form, yupresolver without actually specifying use client
. Although they were mostly using 13.0.5.
以下是我的代码
// React Imports
'use client';
import Link from 'next/link';
import { useForm, Controller } from 'react-hook-form';
import './login.css';
import Image from 'next/image';
// Resolvers & Schema
import { yupResolver } from '@hookform/resolvers/yup';
import { signupSchema } from '@/schema/auth';
// Others
import { headerNameLogo } from '@/EntryFile/imagePath';
import Head from 'next/head';
export default function Login() {
const {
handleSubmit,
control,
setError,
clearErrors,
formState: { errors, isValid },
} = useForm({
mode: 'onChange',
resolver: yupResolver(signupSchema),
});
return (
<>
<Head>
<title>Login - HRXpress</title>
<meta name="description" content="Login page" />
<script src="./togglePassword.ts"></script>
</Head>
<div className="account-content">
<div className="container">
{/* Account Logo */}
<div className="account-logo" style={{ marginTop: 70 }}>
<Link href="/app/main/dashboard">
<Image
style={{ width: 'auto', mixBlendMode: 'darken' }}
src={headerNameLogo}
alt="HRXpress"
/>
</Link>
</div>
{/* /Account Logo */}
<div className="account-box">
<div className="account-wrapper">
<h3 className="account-title">Login</h3>
<p className="account-subtitle">Access to our dashboard</p>
{/* Account Form */}
<div>
<form
onSubmit={(e) => {
e.preventDefault();
}}
>
<div className="form-group">
<label>Email Address</label>
<Controller
name="email"
control={control}
render={({ field: { value, onChange } }) => (
<input
className={`form-control ${
errors?.email ? 'error-input' : ''
}`}
type="text"
value={value}
onChange={onChange}
autoComplete="false"
/>
)}
defaultValue="admin@dreamguys.co.in"
/>
<small>{errors?.email?.message}</small>
</div>
<div className="form-group">
<div className="row">
<div className="col">
<label>Password</label>
</div>
<div className="col-auto">
<Link className="text-muted" href="/forgotpassword">
Forgot password?
</Link>
</div>
</div>
<Controller
name="password"
control={control}
render={({ field: { value, onChange } }) => (
<div className="pass-group">
<input
type="password"
className={`form-control ${
errors?.password ? 'error-input' : ''
}`}
value={value}
onChange={onChange}
autoComplete="false"
/>
<span
id="togglePassword"
className="fa toggle-password fa-eye"
/>
</div>
)}
defaultValue="123456"
/>
<small>{errors?.password?.message}</small>
</div>
<div className="form-group text-center">
<button
disabled={!isValid}
className="btn btn-primary account-btn"
type="submit"
>
Login
</button>
</div>
</form>
<div className="account-footer">
<p>
Don't have an account yet?{' '}
<Link href="/register">Register</Link>
</p>
</div>
</div>
</div>
</div>
</div>
</div>
</>
);
}