6

I am using useFormik hook for my data handling. Adding the data is fine, but when I want to update the data in the same component I need to change the initial values to the custom object instead of having empty strings. Is there formik way of doing that with useFormik Hook? although I can achieve that with states, but then what will be the benefit of using the formik all along. Kindly help!

const formik = useFormik({
        initialValues: {
            name: '',
            post: '',
            contact: '',
            email: '',
            password: '',
        },
        onSubmit: values => {
            //handling submit
        },
    });

Form:

  <form className={classes.form} onSubmit={formik.handleSubmit}>
                    <Grid container spacing={2}>
                        <Grid item xs={12} sm={6}>
                            <TextField
                                autoComplete="name"
                                variant="outlined"
                                required
                                fullWidth
                                label="Name"
                                autoFocus
                                id="name"
                                name="name"
                                onChange={formik.handleChange}
                                value={formik.values.name}
                            />
                        </Grid>
                        <Grid item xs={12} sm={6}>
                            <TextField
                                variant="outlined"
                                required
                                fullWidth
                                label="Post"
                                autoComplete="post"
                                id="post"
                                name="post"
                                onChange={formik.handleChange}
                                value={formik.values.post}

                            />
                        </Grid>
//Submit button and form close etc

Thanks

2
  • Why would you want to update initial values? If you updated initial values they wouldn't just be initial values. Commented Dec 9, 2020 at 7:02
  • Like when a user clicks on the edit icon, the custom values should be populated, thus I want to change the initial values. Commented Dec 9, 2020 at 7:31

1 Answer 1

11

I found the solution that is: For One field

const editEmployee = ()=> {
    formik.setFieldValue("name","Imran");
    alert("changed")     
}

For Multiple:

formik.setValues({"name":"hey","post":"hoii"});

or send the whole Object

Sign up to request clarification or add additional context in comments.

1 Comment

This updates the formik values not initialValues, which is what you asked on the title. Maybe it's fine for your use case but it's not the answer to the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.