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
State-select should be shown only when Country-select equals to 'With States Country 3'.
My first guess was to use Object.fromEntries(myFormContext.getValues())[`todos[${index}].country`] inside FieldArray but that value is updated only the first time Country-select changed and then unpredictably on other form-fields changes for unknown reason.
What is the right way to hide State-select depending on Country-select value inside each of FieldArray entries?
// in Select.tsx
// get select value setter
const [_, setValue] = useControlField(name);
// later react to select change in <select onChange=.... :
onChange={(e) => {
setValue(e.target.value);
validate();
}}
// then in form's `ArrayField` call
const [selectedCountry] = useControlField<Country>(`todos[${index}].country`, 'myForm1');
Initially I thought using Object.fromEntries(myFormContext.getValues())[ `todos[${index}].country ] gives the current value from Select because it did on the first Select change for some reason.
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.
Uh oh!
There was an error while loading. Please reload this page.
-
There are two selects inside FieldArray:
StackBlitz link to the demo:
https://stackblitz.com/edit/node-n5mjdj
State-selectshould be shown only whenCountry-selectequals to 'With States Country 3'.My first guess was to use
Object.fromEntries(myFormContext.getValues())[`todos[${index}].country`]insideFieldArraybut that value is updated only the first timeCountry-selectchanged and then unpredictably on other form-fields changes for unknown reason.What is the right way to hide
State-selectdepending onCountry-selectvalue inside each ofFieldArrayentries?Update:
Finally got it working in https://stackblitz.com/edit/node-m71jxv
Please let me know if solution V1: https://stackblitz.com/edit/node-m71jxv (which seems to be working fine) is the right one to go.
Solved using:
Initially I thought using
Object.fromEntries(myFormContext.getValues())[ `todos[${index}].country ]gives the current value fromSelectbecause it did on the firstSelectchange for some reason.Beta Was this translation helpful? Give feedback.
All reactions