Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset / empty form after submit in Formik

So I've got my form. And I simply want it to be empty after the submit is successfull.

I've seen that I should be using enableReinitializing and change the value manually like : this.values.content = "".

But I'm not managing to understand where can I put this option?

<Formik
    enableReinitializing //This is not working
    initialValues={{
        content: "",
    }}
    validationSchema={validAddMessageToProjectSchema(
        this.props.intl.locale
    )}
    validateOnBlur={true}
    onSubmit={ async ( values: AddMessageToProjectFormValue,
        { setSubmitting }: any
    ) => { await mutate({ variables: values });
        setSubmitting(false);
    }}
>
    {({ isSubmitting, values, errors, touched, setFieldValue }) => {
        return (
            <Form className="addMessageToProject-form">
                <div>
                    <FormattedMessage
                        id="addMessageToProject.descriptionField"
                        defaultMessage="Describe your post"
                    >
                        {msg => (
                            <Field
                                name="content"
                                placeholder={msg}
                                component={
                                    TextAreaField
                                }
                            />
                        )}
                    </FormattedMessage>
                </div>

                <Button
                    type="primary"
                    htmlType="submit"
                    className="addMessageToProject-form__submit form-submit"
                    disabled={isSubmitting}
                >
                    <FormattedMessage
                        id="addMessageToProject.button"
                        defaultMessage="Send Message"
                    />
                </Button>
            </Form>
        );
    }}
</Formik>

like image 576
Baldráni Avatar asked May 17 '19 06:05

Baldráni


People also ask

How do you reset a field in Formik?

If you want to reset the selected value after the form is submitted, you need to provide a controlled value for the Select component. The Formik Field component provides the value in the props object, so you can use it.

What is dirty Formik?

Returns true if values are not deeply equal from initial values, false otherwise. dirty is a readonly computed property and should not be mutated directly.

How do I reset a checkbox in Formik?

You can use Formik's setFieldValue function to reset the field when the checkbox is unchecked.


1 Answers

You can do it like this in onSubmit callback

onSubmit={(values, {setSubmitting, resetForm}) => {
      handleSave(params, () => {
        resetForm(initialValues)
      })

      setSubmitting(false);
    }}

And this is not enableReinitializing instead use enableReinitialize

like image 173
Manoj Kumar Maharana Avatar answered Oct 22 '22 10:10

Manoj Kumar Maharana