TanStackT
TanStack7mo ago
28 replies
brilliant-lime

How to async load defaultValues from a global state?

I should load my defaultValues for useAppForm from a global store.
However, i dont want to block the rendering of the form with a loading or a spinner, so i am doing this:
    useEffect(() => {
        form.setFieldValue('delivery_method', delivery_method)
        form.setFieldValue('recipient_name', recipient_name)
        form.validate('change')
    }, [delivery_method, recipient_name])


This works (even though i don't know if the recommended approach).
However, due to this form.validate(), all non-optional fields with null values shows the error, even tough error content has been guarded by the "field.state.meta.isTouched && !field.state.meta.isValid".

Is this an intended approach? Apparently setFieldValue marks fields touched by default.
Ps: i know setFieldValue has a dontUpdateMeta opt prop, but this seems to not only not marking isTouched: true but also would make form.validate('change') to have no effect.

This seems a much trivial usecase but I am might have not yet found an elegant way to handle it. Appreciate if anyone can shine some light! 🙂
Was this page helpful?