New API - resetField('fieldName')
Discussed in https://github.com/TanStack/form/discussions/1159
Originally posted by wilson12365 February 19, 2025 right now the only way to handle reset a field based on other actions of the form is the following
listeners={{
onChange: ({value}) => {
const defaultMeta = {
isValidating: false,
isTouched: false,
isBlurred: false,
isDirty: false,
errorMap: {}
}
if (value === ‘personal’) {
setFieldValue(‘businessName’, ‘’)
setFieldValue(‘businessLabel’, ‘’)
setFieldMeta(’businessName’, defaultMeta)
setFieldMeta(’businessLabel’, defaultMeta)
}
}}
@crutchcorn has suggested to add resetField('fieldName')
maybe an updateFieldMeta function, setFieldMetaAndValue function and a defaultMeta export would make this a lot more ergonomic but still flexible
@crutchcorn, I could pick this up if you could give a little more detail. I'm back from holidays so I should have some time around the weekend to open a pr
So this is form.resetField() and should revert to the default field value and cleared meta?
@blipk what about my https://github.com/TanStack/form/pull/1125 pr, would that help with the updateFieldMeta part?
@selah-EN-HC-glitch I just released it to main, so update your packages then you can use form.resetField('firstName')
Thank you for adding this! For nested fields, this only works on the leaves , and not on the parent field. Is there a solution that propagates the reset from the parent field to all of its nested fields?
Closed in #1223