Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting setFieldValue to undefined doesn't clear text input #2180

Open
petrbela opened this issue Jan 8, 2020 · 8 comments
Open

Setting setFieldValue to undefined doesn't clear text input #2180

petrbela opened this issue Jan 8, 2020 · 8 comments
Labels

Comments

@petrbela
Copy link

petrbela commented Jan 8, 2020

🐛 Bug report

Current Behavior

When calling setFieldValue('name', undefined), Formik correctly deletes the value from the state but doesn't update the Field value.

Expected behavior

Should clear the text input value.

Reproducible example

https://codesandbox.io/s/formik-setformvalue-to-undefined-doesnt-clear-input-dgqtu

Suggested solution(s)

🤷‍♂️

Additional context

N/A

Your environment

Software Version(s)
Formik 2.1.1
React 16.12.0
TypeScript N/A
Browser Chrome
npm/Yarn N/A
Operating System Mac
@mrmuhammadali
Copy link
Contributor

I'm not sure about your use case but you can try giving an empty string.

@stale stale bot added the stale label Mar 17, 2020
@Mahandrisoa
Copy link

I also faced the same issue, instead it changes the value attribute of the html input element by it doesn't update the UI

@ShaunKT
Copy link

ShaunKT commented Sep 13, 2021

Are there any solutions or work arounds for this issue?

@johnrom
Copy link
Collaborator

johnrom commented Sep 13, 2021

@ShaunKT this is expected behavior. Setting an input's value to undefined sets the input's value={undefined} which in React means it is an uncontrolled input and React will not perform an update on it. Formik is a Controlled Input library unlike something like react-hook-form which uses uncontrolled inputs. A controlled input's empty value should be an empty string, because that is what the user interacts with.

@petrbela
Copy link
Author

@ShaunKT A simple workaround is to call setFieldValue('name', '') followed by setFieldValue('name', undefined). It's not ideal as it causes two rerenders but it should get the job done.

@johnrom
Copy link
Collaborator

johnrom commented Sep 13, 2021

You can also create your own version of Field which does something like:

const TextInput = (fieldProps) => {
  const [field] = useField(fieldProps);

  return <input {...field} value={field.value ?? ''} />
}

Or in v3 you can do:

const formatString = (value) => value ?? '';

const MyForm = () => (
  <Formik {...props}>
    <Field name="myField" format={formatString} />
  </Formik>
)

@thanhnha121
Copy link

@ShaunKT A simple workaround is to call setFieldValue('name', '') followed by setFieldValue('name', undefined). It's not ideal as it causes two rerenders but it should get the job done.

Use NaN worked for my number fields too, setFieldValue('age', NaN)

@CarolynWebster
Copy link

Yup didn't like using NaN for number fields, and I feel like that isn't the same as undefined or null. We should be able to clear a field with undefined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants