Formik: Image drop (upload) with preview + Formik

Created on 10 Aug 2017  路  5Comments  路  Source: formium/formik

Has anyone integrated something like react-dropzone with Formik?

I'm having a hard time getting the pattern right on this one.

Most helpful comment

@mwickett Formik is not designed to do the image upload itself (but you can do it). Instead, my suggestion is to use react-dropzone's onDrop to upload the file to your API media endpoint and then just store the returned file path in Formik state using setFieldValue. You can use something like setStatus() to track the upload's progress/success/failure etc.

All 5 comments

@mwickett Formik is not designed to do the image upload itself (but you can do it). Instead, my suggestion is to use react-dropzone's onDrop to upload the file to your API media endpoint and then just store the returned file path in Formik state using setFieldValue. You can use something like setStatus() to track the upload's progress/success/failure etc.

@jaredpalmer I think setFieldValue only can be called via onChange. CMIIW.

@msmaromi setFieldValue can be used without onChange.

I'm trying to upload an image through Formik with React-Dropzone, this is the main code:

onDrop = ([file]) => {
    const {
      field: { name },
      form: { setFieldValue }
    } = this.props;

    setFieldValue(
      name,
      Object.assign(file, {
        preview: URL.createObjectURL(file)
      })
    );
  };

The part:

Object.assign(file, {
  preview: URL.createObjectURL(file)
})

It's only to do a preview of an image. When I do Click in the handleSubmit, only sends me the preview object, the rest of the info disappear.

gif

screenshot from 2019-02-05 18-50-51

Someone can help me? If you need more info, I can create a issue to explain it bettter I guess...

@MontoyaAndres I faced the same issue. The complete object is contained inside the array, so to use them, just iterate over it:

JSON.stringify(
    {
        files: values.fotos.map(file => ({
            fileName: file.name,
            type: file.type,
            size: `${file.size} bytes`
        }))
    }
)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

sibelius picture sibelius  路  3Comments

pmonty picture pmonty  路  3Comments

dearcodes picture dearcodes  路  3Comments

outaTiME picture outaTiME  路  3Comments

green-pickle picture green-pickle  路  3Comments