I have an update ProfilePage component and I want to preload the form values with the data received from an Axios API request using useQuery, but it seems the useFormik initialValues render before the request is completed therefore having the initial values to be undefined, can anyone help me ?
const { isSuccess, data } = useQuery("author", async () => {
const endPoint = `${baseURL}/auth/author`;
return Axios.get(endPoint, {
headers: {
"x-auth-token": token,
},
})
.then(({ data }) => {
return data.author;
})
.catch((err) => {
setErrors(err.response.data.errors[0].msg);
console.log(errors);
});
});
const formik = useFormik({
initialValues: {
firstname: data.firstname,
lastname: data.lastname,
gender: data.gender,
dob: data.dob,
email: data.email,
oldPassword: "",
newPassword: "",
confirmNewPassword: "",
profilePic: "",
},
validationSchema: Yup.object({
firstname: Yup.string()
.max(20, "First Name must not be more that 20 characters")
.required("Required*"),
lastname: Yup.string()
.max(20, "Last Name must not be more that 20 characters")
.required("Required*"),
gender: Yup.string().required("Required*"),
dob: Yup.string().required("Required*"),
email: Yup.string()
.email("Must be a Valid Email Address")
.required("Required*"),
oldPassword: Yup.string().min(
6,
"Your password must be more than 6 characters"
),
newPassword: Yup.string().min(
6,
"Your password must be more than 6 characters"
),
confirmNewPassword: Yup.string(),
}),
onSubmit: (values) => {
console.log(values);
},
});
The error I get is Cannot read properties of undefined (reading 'firstname')
if I could get a way make useFormik render just after the API request it'd help