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
useFormik() is a custom React hook that will return all Formik state and helpers directly. Despite its name, it is not meant for the majority of use cases. Internally, Formik uses useFormik to create the <Formik> component (which renders a React Context Provider).
dirty: boolean 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.
getFieldProps in React , an alternative for onChange , onBlur and value for a given form field. Posted on September 17, 2020 September 26, 2021 by Banwari Lal.
Now we must write the handleChange method to update the state with user inputs: handleChange = ({ target }) => { const { formValues } = this. state; formValues[target.name] = target. value; this.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With