webdevcody / next-drizzle-lucia-sqlite-template

MIT License
384 stars 96 forks source link

MagicLinkForm causing 'Hydration failed' #13

Open acho1833 opened 4 months ago

acho1833 commented 4 months ago

image image

This is my first time using 'Resend', so it's probably I did something wrong but I just don't even know where to even troubleshoot this issue. Could anyone help?

acho1833 commented 4 months ago

I can troubleshoot to a point that this component is causing the issue.

<MagicLinkForm />
acho1833 commented 4 months ago

Took a while and it turns out my password manager was messing it up. I got the info from

https://stackoverflow.com/questions/75241306/input-field-in-nextjs-causing-hydration-error-and-did-not-expect-server-html-to

So I just turn 'MagicLinkForm' as client component and followed their trick to get it work.

Don't know if this is the best way to solve it but here's how I solved it

'use client';

import { z } from "zod";

import { Input } from "@/components/ui/input";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import {
  Form,
  FormControl,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
} from "@/components/ui/form";
import { signInMagicLinkAction } from "./actions";
import { LoaderButton } from "@/components/loader-button";
import { useServerAction } from "zsa-react";
import { useToast } from "@/components/ui/use-toast";
import { useEffect, useState } from "react";

const magicLinkSchema = z.object({
  email: z.string().email(),
});

export function MagicLinkForm() {
  const { toast } = useToast();
  const [isClient, setIsClient] = useState(false);
  const { execute, isPending } = useServerAction(signInMagicLinkAction, {
    onError({ err }) {
      toast({
        title: "Something went wrong",
        description: err.message,
        variant: "destructive",
      });
    },
  });

  useEffect(() => {
    setIsClient(true);
  }, []);

  const form = useForm<z.infer<typeof magicLinkSchema>>({
    resolver: zodResolver(magicLinkSchema),
    defaultValues: {
      email: "",
    },
  });

  function onSubmit(values: z.infer<typeof magicLinkSchema>) {
    execute(values);
  }

  return (
    <Form {...form}>
      <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8">
        <FormField
          control={form.control}
          name="email"
          render={({ field }) => (
            <FormItem>
              <FormLabel>Email</FormLabel>
              <FormControl>
                {isClient && <Input
                  {...field}
                  className="w-full"
                  placeholder="Enter your email"
                  type="email"
                />}
              </FormControl>
              <FormMessage />
            </FormItem>
          )}
        />
        <LoaderButton isLoading={isPending} className="w-full" type="submit">
          Sign in with magic link
        </LoaderButton>
      </form>
    </Form>
  );
}
delasy commented 4 months ago

rendering email input only if it's on a client-side sounds super weird