Next.js Discord

Discord Forum

Issue using shadcn form builder

Unanswered
West African Lion posted this in #help-forum
Open in Discord
West African LionOP
I'm trying to implement the steps in the shadcn form component, but I'm having an issue that I can't figure out why.
this is my form
import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { transactionSchema } from "@/lib/schemas";
import { Transaction } from "@/lib/types";
import { useCategoriesContext } from "@/lib/hooks";
import {
  Form,
  FormControl,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
} from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import { Button } from "@/components/ui/button";

const TransactionForm = () => {
  const { categories } = useCategoriesContext();
  const form = useForm<Transaction>({
    resolver: zodResolver(transactionSchema),
    defaultValues: {
      categoryId: categories[0]?.id,
      name: "",
      category: "",
      amount: 0.0,
      description: "",
      transactionDate: new Date(),
    },
  });

  function onSubmit(values: Transaction) {
       
    }
  return (
    <Form {...form}>
      <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8">
       <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8">
        <FormField
          control={form.control}
          name="name"
          render={({ field }) => (
            <FormItem>
              <FormLabel>Name</FormLabel>
              <FormControl>
                <Input placeholder="name" {...field} />
              </FormControl>
              <FormMessage />
            </FormItem>
          )}
        />
        <Button type="submit">Submit</Button>
      </form>
        <Button type="submit">Submit</Button>
      </form>
    </Form>
  );
};

export default TransactionForm;
tsx
the error I'm receiving is highlighted when spreading the form variable into the Form component as shown in the image:
Type ...UseFormReturn<Transaction, any, undefined> is not assignable to type React.ReactNode | React.ReactNode[].

11 Replies

@West African Lion I'm trying to implement the steps in the shadcn form component, but I'm having an issue that I can't figure out why. this is my form tsx import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { transactionSchema } from "@/lib/schemas"; import { Transaction } from "@/lib/types"; import { useCategoriesContext } from "@/lib/hooks"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; const TransactionForm = () => { const { categories } = useCategoriesContext(); const form = useForm<Transaction>({ resolver: zodResolver(transactionSchema), defaultValues: { categoryId: categories[0]?.id, name: "", category: "", amount: 0.0, description: "", transactionDate: new Date(), }, }); function onSubmit(values: Transaction) { } return ( <Form {...form}> <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8"> <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8"> <FormField control={form.control} name="name" render={({ field }) => ( <FormItem> <FormLabel>Name</FormLabel> <FormControl> <Input placeholder="name" {...field} /> </FormControl> <FormMessage /> </FormItem> )} /> <Button type="submit">Submit</Button> </form> <Button type="submit">Submit</Button> </form> </Form> ); }; export default TransactionForm; tsx the error I'm receiving is highlighted when spreading the form variable into the Form component as shown in the image: Type ...UseFormReturn<Transaction, any, undefined> is not assignable to type React.ReactNode | React.ReactNode[].
Make sure you remove the other forms. Right now you have 3 forms in one instead of only 1 form. So remove the unneeded onces
West African LionOP
but then it shows a full utilization of the form component here
West African LionOP
I did nothing but copy paste
West African LionOP
but somehow when I open the project in vscode, I don't get that error, I think it could be the ts configuration
or ts extensions? no idea
@West African Lion but then it shows a full utilization of the form component here
Now you have still 2 forms, yes. Remove one more and then you are good to go. When the ts errors are gone when using a different IDE, you might want to take a look into your IDE configuration and maybe even in the „tsconfig“
West African LionOP
how else am I going to submit the form if not for the form tag?
@West African Lion how else am I going to submit the form if not for the form tag?
ah mb. Haven't seen that. Have you done that?
When the ts errors are gone when using a different IDE, you might want to take a look into your IDE configuration and maybe even in the „tsconfig“
West African LionOP
I don't know if I wanna dedicate more time to this because it works perfectly fine in the browser, so I'm just gonna ignore it as ide issue. The problem can't be in the tsconfig because vscode doesn't flag this as an error. Thank you very much for the assistance