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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formik should gracefully handle onChange without a valid event #2189

Open
ajsharp opened this issue Jan 10, 2020 · 2 comments
Open

Formik should gracefully handle onChange without a valid event #2189

ajsharp opened this issue Jan 10, 2020 · 2 comments

Comments

@ajsharp
Copy link

ajsharp commented Jan 10, 2020

馃悰 Bug report

Current Behavior

Some libraries like downshift.js don't give you a native event in the change handler. I'm not a fan of this design decision but it is what it is. So if I manually call onChange on a formik Field with an event, it blows up on this line when it tries to destructure event.target.:

This is the error: Cannot read property 'type' of undefined

So, what I'm doing currently is just passing in an empty target object, and it seems to solve the problem.

Expected behavior

If the code above didn't explode, and instead gracefully handled a null object in event.target.

Reproducible example

https://codesandbox.io/s/formik-codesandbox-template-xnfxx

Suggested solution(s)

Gracefully initialize an empty object if it doesn't exist.

Your environment

Software Version(s)
Formik 2.1.0
React 16.12.0
Browser Chrome Version 79.0.3945.117
npm/Yarn
Operating System Mac OS
@jaredpalmer
Copy link
Owner

This is a 4th or 5th issue around handleChange / event handling / currying since v2. Going to make an umbrella so we can centralize and then debate next steps to minimize api churn

@walkerdb
Copy link

@jaredpalmer I think the main cause of these issues is that v1 technically allowed any object type to be passed in to handleChange, eg this else clause when the value isn't a react event: https://github.com/jaredpalmer/formik/blob/v1.5.8/src/Formik.tsx#L338 .

In v2 this was changed to only accept either a string or a react event, and assumes that any input that isn't a string is a react event, which will break the forms of anyone who happened to be using it assuming it could take any value. This doesn't seem to currently be mentioned as a breaking change in the migration docs.

@stale stale bot removed the stale label May 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants