Skip to content

Use Field Array Context #3942

Answered by charkour
wdfinch asked this question in Q&A
Jan 19, 2021 · 4 comments · 9 replies
Discussion options

You must be logged in to vote

One solution is to create a provider that includes the useForm methods and useFieldArray methods. This adapts the current FormContext code and adds UseFieldArrayMethods to it.

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 e…

Replies: 4 comments 9 replies

Comment options

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

Comment options

You must be logged in to vote
4 replies
@HermanNygaard
Comment options

@charkour
Comment options

@samageloff
Comment options

@Lazercat
Comment options

Answer selected by bluebill1049
Comment options

You must be logged in to vote
4 replies
@bluebill1049
Comment options

@Stevetolba
Comment options

@Moshyfawn
Comment options

@bluebill1049
Comment options

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
10 participants