-
The |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 9 replies
-
That's a really interesting idea. I think we should definitely consider this at v7. |
Beta Was this translation helpful? Give feedback.
-
One solution is to create a provider that includes the type AllFormMethods<TFieldValues extends FieldValues = FieldValues> = UseFormMethods<TFieldValues> &
UseFieldArrayMethods;
const FieldArrayFormContext = React.createContext<AllFormMethods | null>(null);
FieldArrayFormContext.displayName = "RHFArrayContext";
export const useFieldArrayFormContext = <TFieldValues extends FieldValues>(): AllFormMethods<
TFieldValues
> => {
return useContext(FieldArrayFormContext) as AllFormMethods<TFieldValues>;
};
export declare type FieldArrayFormProviderProps<TFieldValues extends FieldValues = FieldValues> = {
children: React.ReactNode;
} & AllFormMethods<TFieldValues>;
export const FieldArrayFormProvider = <TFieldValues extends FieldValues>({
children,
...props
}: FieldArrayFormProviderProps<TFieldValues>) => {
return (
<FieldArrayFormContext.Provider value={{ ...props } as AllFormMethods}>
{children}
</FieldArrayFormContext.Provider>
);
}; usage: const formMethods = useForm({
defaultValues: { "test": {name: "myName" } },
});
const { control, handleSubmit } = formMethods;
const fieldArrayMethods = useFieldArray({
control,
name: "test",
});
return (
<FieldArrayFormProvider {...formMethods} {...fieldArrayMethods}>
<form
onSubmit={handleSubmit((data) => {
// TODO: handle this.
console.log(data);
})}
>
...
</form>
</FieldArrayFormProvider>
);
}; |
Beta Was this translation helpful? Give feedback.
-
I see that this was not included in v7. Is it still being considered as a feature or should we accept the workaround as a final solution to this problem? |
Beta Was this translation helpful? Give feedback.
-
This answer helped me a lot! In RHF ChangeLog ## [v7.0.0-alpha.2] - 2021-02-04 there are name changes:
Not greate for TS, but this workin for me now:
It’s a pity that your decision was not accepted by the RHF @bluebill1049 |
Beta Was this translation helpful? Give feedback.
One solution is to create a provider that includes the
useForm
methods anduseFieldArray
methods. This adapts the currentFormContext
code and addsUseFieldArrayMethods
to it.