question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Old error shown when setFieldValue and setFieldTouched are executed together

See original GitHub issue

After executing the following code, Formik shows an error based on the previous value. The order of the two lines doesn’t make a difference.

this.props.setFieldValue('rating', value);
this.props.setFieldTouched('rating', true);

Wrapping setFieldTouched in setImmediate solves the problem so I presume it’s some kind of race condition?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
delphin92commented, Feb 18, 2020

I still have this issue:

/* Custom field component */
const [field, meta, {setValue, setTouched}] = useField({name: controlId, ...props});
const change = (year, month) => {
        setValue({year: year, month: month});
        setTouched(true);
    };

/* Validation */
yup.addMethod(yup.mixed, 'monthYearLessThan', function (monthYear, message) {
    return this.test('monthYearLessThan', message, function (value) {
        console.log(value);
        return isMonthYearLess(value, this.parent[monthYear]);
    })
});

I am changing the value from 2016-01 to 2020-02. The validation was called twice, with new and old value:

{year: 2020, month: 2}
{year: 2016, month: 1}

So, field was marked as valid, that is incorrect.

I was forced to WA:

setValue({year: year, month: month});
setFieldTouched(name, true, false);
1reaction
codeincontextcommented, Jul 31, 2017

No, this is in the browser. I’m using a custom StarRating component and the change is triggered by an onClick under the hood. I assume it would work properly with radio elements and their onChange, then On Mon, 31 Jul 2017 at 16:31 Jared Palmer notifications@github.com wrote:

Are you in React Native land? In the browser, React batches update to state in response to a change/blur event. Not sure if this behavior is mimicked in RN.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jaredpalmer/formik/issues/106#issuecomment-319187517, or mute the thread https://github.com/notifications/unsubscribe-auth/AAFj68pQy6TjkqbJCwbPcDC0FnbVHPZeks5sTjmsgaJpZM4Oo4WN .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issue with values Formik - Stack Overflow
When you call getValues , it gets old value as setFieldValue ... I had a simmilar problem connected to setFieldValue and setFieldTouched ....
Read more >
formik onchange not working
GitHub Bug report Current Behavior Form not validating on change, ... After executing the following code, Formik shows an error based on the...
Read more >
Build and manage form using Formik and Yup, in React Native
};const EditProfile = props => { return ( <Formik> {({ dirty, values, setFieldValue, errors, touched, setFieldTouched, isValid,
Read more >
withFormik() | Formik
If specified, your wrapped form will show up as Formik(displayName) . ... reset props.errors to this initial value (and this function will be...
Read more >
Build Your Own Formik Using React Hooks with Jared Palmer
onBlur should be used over onMouseDown so that errors aren't shown before the ... tells it to not run if the value hasn't...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found