我刚刚开始了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. enter image description here

以下是我的代码

// 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>
        </>
    );
}

推荐答案

这是因为在最新版本的nextJS中,无论您使用了什么指令,每个组件都首先在服务器端呈现.有关更多信息,请参阅here.

Node.js相关问答推荐

无法验证叶签名|无法验证第一个证书

postgresql层与应用层的序列化

Mongoose查询-如何根据当前查找ID获取其他集合并将其插入到当前查找中?

(0,core_1.default)不是使用@middy/core的lambda处理程序上的函数

模块';"; node :流程&没有导出的成员';dlopen';

将 POST 的 json 变量格式化为 lambda

我如何在nodejs中的路由之间交换令牌

如何从mongoose 对象内嵌套的数组中提取数组元素?

npm install 在 Mac 上的 Node-gyp 构建错误

在快速路由中使用 axios 会在数据字段中返回特殊字符而不是 json

kubernetes 上的 nextjs 无法启动

Pug - 包括带有include关键字的c代码

如何更改NodeJS中的堆栈大小限制?

在 ExpressJS 中将变量传递给 JavaScript

与 NPM 一起安装时找不到 Express 模块

调用 require 时的 const vs let

异步函数 - 等待不等待promise

如何在客户端使用 node.js 模块系统

如何创建安全(TLS/SSL)Websocket 服务器

'node' 未被识别为内部或外部命令