You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As the docs clearly state, no two useFieldArray for the same field can be instantiated, as each handles it's own snapshot of state internally.
However, when building large multi-page or multi-step forms, we like to lean in to hooks and context providers so we aren't passing a ton of props to deeply nested components. Being able to call a useFieldArray twice for the same field, so that different form pages can update the same field array, would be a nice quality of life improvement. 100% an "opt-in" feature, as I know it de-optimizes rerenders.
The way we handle it today is by creating our own react context and providing the results of any fieldArrays we want to use. It works, it's just a little boilerplate-y.
Example:
constFormFieldArrayCtx=React.createContext<CtxVal|null>(null);exportconstFooFormProvider: React.FC<{children: React.ReactNode;}>=({ children })=>{constformMethods=useForm<FooForm>();const{ control }=formMethods;constpersonsArray=useFieldArray({ control,name: 'people'});consttransactionsArray=useFieldArray({ control,name: 'transactions'});constctxVal=useMemo<CtxVal>(()=>({
personsArray,
transactionsArray,}),[personsArray,transactionsArray]);return(<FormProvider{...formMethods}><FormFieldArrayCtx.Providervalue={ctxVal}>{children}</FormFieldArrayCtx.Provider></FormProvider>);};/** * Access any react-hook-form methods on the Foo Form */exportconstuseFooForm=()=>{constmethods=useFormContext<FooForm>();constfieldArrays=useContext(FormFieldArrayCtx);if(!fieldArrays){thrownewError('useFooForm can only be used inside of a FooFormProvider');}return{
...methods,
...fieldArrays,};};
Something like a reactive: true in the useFieldArray options would be a nice touch, and should be relatively simple to implement by having the internals of useFieldArray just subscribe/watch the values of the key it was provided. I took a look at source code and seems relatively simple, but I am interested in overall feedback/criticism of this approach.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
As the docs clearly state, no two useFieldArray for the same field can be instantiated, as each handles it's own snapshot of state internally.
However, when building large multi-page or multi-step forms, we like to lean in to hooks and context providers so we aren't passing a ton of props to deeply nested components. Being able to call a useFieldArray twice for the same field, so that different form pages can update the same field array, would be a nice quality of life improvement. 100% an "opt-in" feature, as I know it de-optimizes rerenders.
The way we handle it today is by creating our own react context and providing the results of any fieldArrays we want to use. It works, it's just a little boilerplate-y.
Example:
Something like a
reactive: true
in the useFieldArray options would be a nice touch, and should be relatively simple to implement by having the internals of useFieldArray just subscribe/watch the values of the key it was provided. I took a look at source code and seems relatively simple, but I am interested in overall feedback/criticism of this approach.Thanks!
Beta Was this translation helpful? Give feedback.
All reactions