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

MagicLinkForm causing 'Hydration failed' #13

Open
acho1833 opened this issue Jun 29, 2024 · 3 comments
Open

MagicLinkForm causing 'Hydration failed' #13

acho1833 opened this issue Jun 29, 2024 · 3 comments

Comments

@acho1833
Copy link

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
Copy link
Author

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

<MagicLinkForm />

@acho1833
Copy link
Author

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
Copy link
Contributor

delasy commented Jul 2, 2024

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

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

2 participants