Skip to content

How do I use useFieldArray with a generic Control Type? #11708

Answered by sujinleeme
HvZimmermann asked this question in Q&A
Discussion options

You must be logged in to vote

I grabbed the hint from here.

[playground]

import React from 'react';
import { Control, useFieldArray, useForm } from "react-hook-form";

type GenericFormData = {
  theFieldICareAbout: { name: string }[];
};

type FirstFormValues = {
  task: string;
} & GenericFormData;

type SecondFormValues = GenericFormData;

type GenericComponentProps<TFormValues> = {
  control: TFormValues extends GenericFormData ? Control<TFormValues> : never;
};

function GenericComponent<TFormValues extends GenericFormData>({
  control,
}: GenericComponentProps<TFormValues>) {
  const { append } = useFieldArray({
    control,
    name: "theFieldICareAbout",
  });

  const onAppendQuestionGroup = () =>
    append({

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@HvZimmermann
Comment options

Answer selected by HvZimmermann
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants