question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

initialize react dropzone with file

See original GitHub issue

I am using redux form for form related task. I am using react-dropzone for uploading files and redux form is used along with it. I could submit file to server but could not show that submitted file as initialValues for upload field.

here is how i have done

const Form = ({ item }) => {
    return (
        <>
            <form onSubmit={handleSubmit(val => handleItemSubmit(val, mutation))}>
                <Field name="photo" component={UploadField} />
            </form>
        </>
    )
}

export default compose(
  connect((state, props) => {
    const { items } = props;
    return {
      initialValues: {
        ...items,
        photo:
          items && items.photoPath
            ? { name: items.photoName, size: 12345 }
            : null,
        languages:
          items &&
          items.languages &&
          get(items, "languages", "")
            .split(",")
            .map(lang => ({
              label: lang,
              value: lang
            }))
      }
    };
  }),
  reduxForm({
    form: "item_form",
    enableReinitialize: true,
    fields: { ...requiredFields },
    validate
  })
)(Form);



import React from "react";
import styled from "styled-components";
import { useDropzone } from "react-dropzone";

export const UploadField = props => {
  const {
    input: { onChange },
    disabled
  } = props;

  const { getRootProps, getInputProps } = useDropzone({
    onDrop: files => onChange(files)
  });

  const files = props.input.value;

  if (disabled) {
    return null;
  }

  return (
    <>
      <DropzoneContainer {...getRootProps()}>
        <input {...getInputProps()} />
        Upload files here!
      </DropzoneContainer>
      <br />
      <div>
        {Object.keys(files).map((key, index) => {
          console.log("FILES ---", files);
          return (
            <ThumbsContainer>
              <p>{files[key].name}</p>
            </ThumbsContainer>
          );
        })}
      </div>
    </>
  );
};

export default UploadField;

const DropzoneContainer = styled.div`
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 20px;
    border-width: 2px;
    border-radius: 2px;
    border-color: ${props => getColor(props)};
    border-style: dashed;
    background-color: #fafafa;
    color: #bdbdbd;
    outline: none;
    transition: border 0.24s ease-in-out;
  `,
  ThumbsContainer = styled.aside`
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    margin-top: 16px;
  `,
  Thumb = styled.div`
    display: inline-flex;
    border-radius: 2px;
    border: solid 1px #eaeaea;
    margin-bottom: 8px;
    width: 100;
    height: 100;
    padding: 4px;
    box-sizing: border-box;
  `,
  ThumbInner = styled.div`
    display: flex;
    min-width: 0;
    overflow: hidden;
  `,
  Image = styled.img`
    display: block;
    max-width: 100%;
    width: 180px;
    height: 180px;
  `;

const getColor = props => {
  if (props.isDragAccept) {
    return "#00e676";
  }
  if (props.isDragReject) {
    return "#ff1744";
  }
  if (props.isDragActive) {
    return "#2196f3";
  }
  return "#eeeeee";
};

I get these two fields back from server when uploading images.

Image of upload result

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8

github_iconTop GitHub Comments

2reactions
danielprettcampagna-ipirangacommented, Sep 20, 2022

Shouldn’t this issue be reopened?

1reaction
SanskarSanscommented, Dec 10, 2019

what kind of file has to be passed? Can you show me an example, please?

Read more comments on GitHub >

github_iconTop Results From Across the Web

initialize react dropzone with file - Stack Overflow
I am using redux form for form related task. I am using react-dropzone for uploading files and redux form is used along with...
Read more >
React Dropzone example: Multiple Files upload ... - BezKoder
In this React tutorial, I will show you way to build React Dropzone Multiple Files upload example using react-dropzone for drag & drop...
Read more >
Create a drag-and-drop with react-dropzone - LogRocket Blog
Learn how to use react-dropzone to create a drag-and-drop component for uploading images. Compare this to the HTML Drag and Drop API method....
Read more >
Creating a File Dropzone with React - malcoded
In this tutorial you are going to learn how create a file dropzone component from scratch using react. We will discover how to...
Read more >
react-dropzone
Files ;; import · 'react-dropzone' ; // Note that there will be nothing logged when files are dropped <Dropzone onDrop={files => console.log(files)}> {...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found