Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-dropzone

Package Overview
Dependencies
Maintainers
3
Versions
189
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-dropzone

Simple HTML5 drag-drop zone with React.js

  • 14.3.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.7M
decreased by-19.27%
Maintainers
3
Weekly downloads
 
Created

What is react-dropzone?

The react-dropzone npm package is a simple and highly customizable dropzone component for React applications. It allows users to drag and drop files into an area of a web page or click to select files through the file explorer. It is designed to provide developers with a flexible and easy-to-use interface for handling file uploads.

What are react-dropzone's main functionalities?

Basic File Drop

This feature allows users to create a basic dropzone area where files can be dragged and dropped or selected through a file dialog.

import React from 'react';
import { useDropzone } from 'react-dropzone';

function BasicDropzone() {
  const { getRootProps, getInputProps } = useDropzone();

  return (
    <div {...getRootProps()}>
      <input {...getInputProps()} />
      <p>Drag 'n' drop some files here, or click to select files</p>
    </div>
  );
}

export default BasicDropzone;

Accepting Specific File Types

This feature allows developers to specify which file types the dropzone will accept, limiting the user to only select or drag those types.

import React from 'react';
import { useDropzone } from 'react-dropzone';

function SpecificTypeDropzone() {
  const { getRootProps, getInputProps } = useDropzone({
    accept: 'image/*'
  });

  return (
    <div {...getRootProps()}>
      <input {...getInputProps()} />
      <p>Drag 'n' drop some images here, or click to select images</p>
    </div>
  );
}

export default SpecificTypeDropzone;

Styling Dropzone Based on Drag State

This feature allows developers to apply different styles to the dropzone depending on whether files are being dragged over it.

import React from 'react';
import { useDropzone } from 'react-dropzone';

function StyledDropzone() {
  const { getRootProps, getInputProps, isDragActive } = useDropzone();

  return (
    <div {...getRootProps()} style={{
      border: '2px dashed #eeeeee',
      backgroundColor: isDragActive ? '#e0e0e0' : '#fafafa',
      padding: '20px'
    }}>
      <input {...getInputProps()} />
      {
        isDragActive ?
          <p>Drop the files here ...</p> :
          <p>Drag 'n' drop some files here, or click to select files</p>
      }
    </div>
  );
}

export default StyledDropzone;

Handling File Previews

This feature allows developers to handle file previews by creating image thumbnails for dropped files.

import React, { useState } from 'react';
import { useDropzone } from 'react-dropzone';

function FilePreviewDropzone() {
  const [files, setFiles] = useState([]);
  const { getRootProps, getInputProps } = useDropzone({
    onDrop: acceptedFiles => {
      setFiles(acceptedFiles.map(file => Object.assign(file, {
        preview: URL.createObjectURL(file)
      })));
    }
  });

  const images = files.map(file => (
    <div key={file.name}>
      <img src={file.preview} style={{ width: '200px' }} alt='preview' />
    </div>
  ));

  return (
    <section className='container'>
      <div {...getRootProps()}>
        <input {...getInputProps()} />
        <p>Drag 'n' drop some files here, or click to select files</p>
      </div>
      <aside>
        <h4>Files</h4>
        {images}
      </aside>
    </section>
  );
}

export default FilePreviewDropzone;

Other packages similar to react-dropzone

Keywords

FAQs

Package last updated on 04 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc