How to use react-dropzone module in ReactJS ?
Last Updated :
30 Jul, 2024
React-Dropzone module is a simple React hook that is used to create an HTML5-compliant drag-drop zone for n number of files. We can use this module to provide a way for users to drop and drop their multiple files, and then we can handle these files as per the business requirement.
Prerequisites:
Approach:
The integration of the "react-dropzone" module into a ReactJS application. The "useDropzone" wrapper component is imported to obtain dropzone property getters. These getters are applied to an Input element, creating a drag-and-drop zone. Clicking on the text "Click to select the file or drag-and-drop the file here!!" prompts the user to select a file, enabling subsequent operations based on business requirements.
Steps to Create the React Application And Installing Module:
Step 1: Create a React application using the following command:
npx create-react-app foldername
Step 2: After creating your project folder i.e. foldername, move to it using the following command:
cd foldername
Step 3: Install the required module using the following command:
npm install react-dropzone
Project Structure:
Project StructureThe update dependencies in package.json file will look like:
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-dropzone": "^14.2.3",,
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4",
}
Example: Now write down the following code in the App.js file
App.js
import React, { useCallback } from 'react'
import { useDropzone } from 'react-dropzone'
export default function App() {
const onDrop = useCallback(acceptedFiles => {
alert(acceptedFiles[0].name)
console.log("Now you can do anything with"+
" this file as per your requirement")
}, [])
const { getInputProps, getRootProps } = useDropzone({ onDrop })
return (
<div style={{ display: 'block', width: 700, padding: 30 }}>
<h4>React-Dropzone Module Demo</h4>
<div {...getRootProps()}>
<input {...getInputProps()} />
<p>Click to select file or
drag-and-drop the file here!!</p>
</div>
</div>
);
}
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output: Now open your browser and go to http://localhost:3000
Similar Reads
How to use react-draggable module in React JS ? In React JS, the react-draggable module facilitates element dragging by utilizing CSS Transforms, enabling items to be draggable from any starting position. To implement this, the following approach can be employed in ReactJS.Prerequisites:React JSReact Functional ComponentApproach to use react-drag
2 min read
How to Use react-select in React Application? React Select is a flexible and handy dropdown library for React that handles multi-select, loading data asynchronously, custom options, and more in your components with ease. In this article, we will see how to use react-select in React Application. PrerequisiteNode.js installednpm or yarn package m
2 min read
How to use Slide Component in ReactJS ? Slide Component adds a Slide animation to a child element or component. Material UI for React has this component available for us, and it is very easy to integrate. We can use the Slide Component in ReactJS using the following approach.Prerequisites:NodeJS or NPMReactJSMaterial UISteps to Create the
3 min read
ReactJS Semantic UI Dropdown Module Semantic UI is a modern framework used in developing seamless designs for the website, Its gives the user a lightweight experience with its components. It uses the predefined CSS, JQuery language to incorporate in different frameworks. In this article we see know how to use Dropdown Module in ReactJ
3 min read
How to use Select Component in Material UI ? Select components are used for collecting user-provided information from a list of options. Material UI for React has this component available for us and it is very easy to integrate. We can use the Select Component in ReactJS using the following approach:Creating React Application And Installing Mo
2 min read