jaredpalmer / formik

Build forms in React, without the tears 😭
https://formik.org
Apache License 2.0
33.97k stars 2.79k forks source link

How to use setFieldValue without any event calls (onChange onBlur ..) #2489

Open SHUJAAT-DEV opened 4 years ago

SHUJAAT-DEV commented 4 years ago

❓Question

In my situation, I need to set Select value on some conditions, the setFieldValue(name, values) cause a loop. I need help to handle such a situation where you need to set the value without using any event calls. your response highly appreciated.

if (val) { setFieldValue(name, { label: val[0].concept.display, value: val[0].concept.uuid })

vikingair commented 4 years ago

It's not clear where you're calling the above code. If you're calling it inside of a function that returns the render result, you cause site effects.

Maybe what you actually need is something like:

useEffect(() => {
    if (val) setFieldValue(name, { label: val[0].concept.display, value: val[0].concept.uuid });
}, [val]);
hrgui commented 4 years ago

To use setFieldValue without using any event calls:


setFieldValue cannot be called outside of Formik's scope (for example: through a Redux thunk).