@KyuubiTila
To fix this issue and correctly handle file uploads, you can make use of the onFileChange function provided by Formik. Here's how you can modify your CreateProductCard component to handle file uploads correctly:
Ths is what my code looks like
'use client';
import React from 'react';
import { Formik, Form, Field } from 'formik';
import FileUpload from '@/components/fileupload';
Bug report
import React from 'react'; import { Formik, Form, Field, ErrorMessage } from 'formik';
const CreateProductCard = ({ addProduct, initialValues, validationSchema }) => { return ( <Formik initialValues={initialValues} onSubmit={(data, params) => { console.log(data); // addProduct(data); // params.resetForm(); }} validationSchema={validationSchema}
@KyuubiTila To fix this issue and correctly handle file uploads, you can make use of the
onFileChange
function provided by Formik. Here's how you can modify yourCreateProductCard
component to handle file uploads correctly:still no way out, formik seems to not be sending the image as a file object.
yeahhh so here is the solution that works. Just figured it out
FIRST THING TO DO You can create one component like this, just a raw component.
import React from 'react';
function FileUpload(props) { const {field, form} = props;
const handleChange = (e) => { const file = e.currentTarget.files[0]; const reader = new FileReader(); const imgTag = document.getElementById("myimage"); imgTag.title = file.name; reader.onload = function(event) { imgTag.src = event.target.result; }; reader.readAsDataURL(file); form.setFieldValue(field.name, file); };
return (
); }
export default FileUpload;
SECONDLY, IMPORT IN YOUR FORMIK CONTROLLED FORM
And use this like this inside the form
<Field name="image" component={FileUpload} // import from the component you just created above />
SOLUTION CREDIT : Sonukr, on Apr 14, 2019
Ths is what my code looks like 'use client'; import React from 'react'; import { Formik, Form, Field } from 'formik'; import FileUpload from '@/components/fileupload';
const UpdateProfile = () => { const initialValues = { username: '', bio: '', profilePhoto: '', };
const handleSubmit = (values) => { console.log('Form values:', values); };
return (
); };
export default UpdateProfile;
and the component
import React from 'react';
function FileUpload(props) { const { field, form } = props;
const handleChange = (e) => { const file = e.currentTarget.files[0]; const reader = new FileReader(); const imgTag = document.getElementById('myimage'); imgTag.title = file.name; reader.onload = function (event) { imgTag.src = event.target.result; }; reader.readAsDataURL(file); form.setFieldValue(field.name, file); };
return (
); }
export default FileUpload;
AND HERE IS THE RESULT: