Workaround for FieldArray not updating isValid? #2656
Unanswered
tyteen4a03
asked this question in
General
Replies: 1 comment
-
|
The issue (#1616) is that Replace import { useFormikContext, FieldArray } from 'formik';
function MyFieldArray() {
const { values, validateForm } = useFormikContext();
return (
<FieldArray name="items">
{(arrayHelpers) => (
<div>
{values.items.map((item, index) => (
<div key={index}>
<Field name={`items.${index}.name`} />
<button
type="button"
onClick={async () => {
arrayHelpers.remove(index);
// Force revalidation after removal
await Promise.resolve(); // wait one tick for state to update
validateForm();
}}
>
Remove
</button>
</div>
))}
<button type="button" onClick={() => arrayHelpers.push({ name: '' })}>
Add
</button>
</div>
)}
</FieldArray>
);
}Why Alternative — use the <FieldArray
name="items"
validate={(items) => {
// runs on every change including removes
if (!items || items.length === 0) return 'At least one item required';
}}
>The |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Is there a workaround for this issue? #1616
I know there's this comment but I'm confused as to where it goes and how it replaces my normal
remove().Beta Was this translation helpful? Give feedback.
All reactions