How To Drag and Drop Files in React Applications with react-dropzone
Adding files to upload is a common task in React applications. In this article you will learn how to easily add a drop zone for files in React projects with react-dropzone. We will see how to add react-dropzone to a project, configure and apply styling to your drop zone.The react-dropzone allows to create drag-drop zones for files in React applications. You can add files by dropping them or via a file dialog. For this article, we assume that you're familiar with React components and how to use props and state to control them. Let's start by adding react-dropzone to a project: Now we can use the Dropzone component from the react-dropzone package to add a drop zone for files. We will show a list of added files in a list: We start by importing the Dropzone component. Next, we use it to render our drop zone for files. We specify the handleDrop handler that will process dropped files with the onDrop property. Also, we specify a function as the Dropzone component children that renders a drop zone. This function receives two arguments: getRootProps and getInputProps . These functions return properties that should be applied to drop zone elements. The Dropzone component helps us to create a drop zone but we are free to render any layout we want. The only requirement is to apply properties returned by getRootProps to a root element and apply properties returned by getInputProps to an <input> element. If we want to specify additional properties to root or input elements, we need to pass them to props functions. In our example, we do so by specifying the value for the className property to the getRootProps function. This is required to ensure that properties do not get overridden. When we add files to a drop zone, the handleDrop handler will be called. We use the useState hook to store names of added files in a state. We use the fileNames state variable during render to display a list of added files. In this article, we'll be using the Dropzone component for drop zones. If you prefer working with hooks over components, you may use the useDropzone hook to achieve the same functionality. The Dropzone component is just a wrapper over the useDropzone hook that makes it easier to use react-dropzone. The react-dropzone provides configuration options that allow controlling the behavior of a drop zone. You can check out the API description to review available options. We can configure a drop zone to accept single or multiple files, specify a minimum and maximum size for files to accept, limit accepted files by MIME type or extension, and get notified about dragging events. Let's configure a drop zone to accept only image files between 10 Kb and 3 Mb: We specify additional properties for the Dropzone component to configure our drop zone. The minSize and maxSize properties specify a minimum and maximum file size, and the accept property allows to specify types of files to accept. It is up to you to style a drop zone when using react-dropzone. The react-dropzone does not provide any default styling. However, it makes it easy to dynamically apply styles depending on a drag-drop events. You can define your styling to match the design of your application. For example, this is the CSS class for a drop zone that we've used in previous examples: To dynamically apply additional styling, we may use properties provided to the rendering function in the Dropzone component. Let's change the border of a drop zone depending on whether files will be accepted by the drop zone: In this example, we're using the isDragActive , isDragAccept , and isDragReject properties provided to the rendering function to change the look of a drop zone. When a file complies with a drop zone configuration (in our case, image file with a specific size), the isDragAccept property will be true . If a file will not be accepted, the isDragReject property will be true . The isDragActive property will be true while user drags files over a drop zone. The react-dropzone is an easy way to add a drop zone for files in your React applications. It is easy to use and provides a lot of configuration options.