Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Next.js Text content does not match server-rendered HTML. #178

Open
parkgang opened this issue Nov 16, 2022 · 5 comments
Open

Next.js Text content does not match server-rendered HTML. #178

parkgang opened this issue Nov 16, 2022 · 5 comments

Comments

@parkgang
Copy link

Overview

The problem occurs in the versions below.

"next": "^12.3.1"
"react": "18.2.0"
"react-dom": "18.2.0"

An easy setup for that test is to run the command below.

npx create-next-app@latest --ts

error symptom

image

Resolution

I downgraded the react version to 17 and the problem did not occur.
Something seems to be a problem between react@18 and next.js.

"react": "^17.0.2",
"react-dom": "^17.0.2",
@bluebill1049
Copy link
Member

you could use next/dynamic ssr:false with nextjs

@parkgang
Copy link
Author

parkgang commented Nov 16, 2022

you could use next/dynamic ssr:false with nextjs

I want to do something, but I get a type error. Do you know the expected reason?

const DevTool = dynamic(
  () => import("@hookform/devtools").then((mod) => mod.DevTool),
  {
    ssr: false,
  }
);

image

I followed the official document of Next.js, but I have a question because I couldn't infer it as a component.

@parkgang
Copy link
Author

parkgang commented Nov 16, 2022

To check if the grammar was wrong, I created a Test Component component and proceeded cleanly as follows, but no problem occurred.

import { useForm, SubmitHandler } from "react-hook-form";
import dynamic from "next/dynamic";

const Test = dynamic(() => import("./Test").then((mod) => mod.Test), {
  ssr: false,
});
const DevTool = dynamic(
  () => import("@hookform/devtools").then((mod) => mod.DevTool),
  {
    ssr: false,
  }
);

type Inputs = {
  example: string;
  exampleRequired: string;
};

export default function Home() {
  const {
    register,
    control,
    handleSubmit,
    watch,
    formState: { errors },
  } = useForm<Inputs>();
  const onSubmit: SubmitHandler<Inputs> = (data) => console.log(data);

  console.log(watch("example"));

  return (
    <>
      <form onSubmit={handleSubmit(onSubmit)}>
        <input defaultValue="test" {...register("example")} />
        <input {...register("exampleRequired", { required: true })} />
        {errors.exampleRequired && <span>This field is required</span>}
        <input type="submit" />
      </form>
      {/* OK */}
      <Test />
      {/* Type Error */}
      <DevTool control={control} />
    </>
  );
}

I suspect something is wrong with the types below.

image

@parkgang
Copy link
Author

I haven't been able to find a solution other than changing the state so that it can only be displayed on the Client Side .

This is a cumbersome code base that you have to go through every time you use DevTool .

I don't think this is the best way.

import { useForm, SubmitHandler } from "react-hook-form";
import { DevTool } from "@hookform/devtools";
import { useEffect, useState } from "react";

type Inputs = {
  example: string;
  exampleRequired: string;
};

export default function Home() {
  const {
    register,
    control,
    handleSubmit,
    watch,
    formState: { errors },
  } = useForm<Inputs>();

  const onSubmit: SubmitHandler<Inputs> = (data) => console.log(data);

  console.log(watch("example"));

  // Control with state to render only on the Client Side
  const [isDevToolEnabled, setIsDevToolEnabled] = useState<boolean>(false);
  useEffect(() => {
    setIsDevToolEnabled(true);
  }, []);

  return (
    <>
      <form onSubmit={handleSubmit(onSubmit)}>
        <input defaultValue="test" {...register("example")} />
        <input {...register("exampleRequired", { required: true })} />
        {errors.exampleRequired && <span>This field is required</span>}
        <input type="submit" />
      </form>

      {/* I didn't want to create a state, so I tried the code below, but it caused a problem */}
      {/* {typeof window !== "undefined" && <DevTool control={control} />} */}

      {/* The solution is to display it on the screen by changing the state in the case of the client side as follows */}
      {isDevToolEnabled && <DevTool control={control} />}
    </>
  );
}

@manudeli
Copy link

you could use next/dynamic ssr:false with nextjs

I want to do something, but I get a type error. Do you know the expected reason?

const DevTool = dynamic(
  () => import("@hookform/devtools").then((mod) => mod.DevTool),
  {
    ssr: false,
  }
);

image

I followed the official document of Next.js, but I have a question because I couldn't infer it as a component.

I met Same problem. even if I use dynamic as @bluebill1049's suggestion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants