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

react-images-uploading

Package Overview
Dependencies
Maintainers
2
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-images-uploading

The simple images uploader applied Render Props pattern. This approach allows you to fully control UI component and behaviours.

  • 3.1.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
31K
decreased by-0.21%
Maintainers
2
Weekly downloads
 
Created
Source

react-images-uploading

The simple images uploader applied Render Props pattern. (You can read more about this pattern here).

This approach allows you to fully control UI component and behaviours.

See the intro blog post

tested with jest Github_Actions NPM NPM License: MIT PRs Welcome Package Quality code style: prettier

All Contributors

Package Quality

Installation

npm

npm install --save react-images-uploading

yarn

yarn add react-images-uploading

Usage

You can check out the basic demo here:

Basic

import React from 'react';
import ImageUploading from 'react-images-uploading';

export function App() {
  const [images, setImages] = React.useState([]);
  const maxNumber = 69;

  const onChange = (imageList, addUpdateIndex) => {
    // data for submit
    console.log(imageList, addUpdateIndex);
    setImages(imageList);
  };

  return (
    <div className="App">
      <ImageUploading
        multiple
        value={images}
        onChange={onChange}
        maxNumber={maxNumber}
        dataURLKey="data_url"
      >
        {({
          imageList,
          onImageUpload,
          onImageRemoveAll,
          onImageUpdate,
          onImageRemove,
          isDragging,
          dragProps,
        }) => (
          // write your building UI
          <div className="upload__image-wrapper">
            <button
              style={isDragging ? { color: 'red' } : undefined}
              onClick={onImageUpload}
              {...dragProps}
            >
              Click or Drop here
            </button>
            &nbsp;
            <button onClick={onImageRemoveAll}>Remove all images</button>
            {imageList.map((image, index) => (
              <div key={index} className="image-item">
                <img src={image['data_url']} alt="" width="100" />
                <div className="image-item__btn-wrapper">
                  <button onClick={() => onImageUpdate(index)}>Update</button>
                  <button onClick={() => onImageRemove(index)}>Remove</button>
                </div>
              </div>
            ))}
          </div>
        )}
      </ImageUploading>
    </div>
  );
}

Validation

...
  {({ imageList, onImageUpload, onImageRemoveAll, errors }) => (
    errors && <div>
      {errors.maxNumber && <span>Number of selected images exceed maxNumber</span>}
      {errors.acceptType && <span>Your selected file type is not allow</span>}
      {errors.maxFileSize && <span>Selected file size exceed maxFileSize</span>}
      {errors.resolution && <span>Selected file is not match your desired resolution</span>}
    </div>
  )}
...

Drag and Drop

...
  {({ imageList, dragProps, isDragging }) => (
    <div {...dragProps}>
      {isDragging ? "Drop here please" : "Upload space"}
      {imageList.map((image, index) => (
        <img key={index} src={image.data_url} />
      ))}
    </div>
  )}
...

Props

parametertypeoptionsdefaultdescription
valuearray[]List of images
onChangefunction(imageList, addUpdateIndex) => voidCalled when add, update or delete action is called
dataURLKeystringdataURLCustomized field name that base64 of selected image is assigned to
multiplebooleanfalseSet true for multiple chooses
maxNumbernumber1000Number of images user can select if mode = multiple
onErrorfunction(errors, files) => voidCalled when it has errors
acceptTypearray['jpg', 'gif', 'png'][]The file extension(s) to upload
maxFileSizenumberMax image size (Byte) and it is used in validation
resolutionTypestring'absolute' | 'less' | 'more' | 'ratio'Using for image validation with provided width & height
resolutionWidthnumber> 0
resolutionHeightnumber> 0
inputPropsReact.HTMLProps<HTMLInputElement>

Note

resolutionType

valuedescription
absoluteimage's width === resolutionWidth && image's height === resolutionHeight
ratio(resolutionWidth / resolutionHeight) === (image width / image height)
lessimage's width < resolutionWidth && image's height < resolutionHeight
moreimage's width > resolutionWidth && image's height > resolutionHeight

Exported options

parametertypedescription
imageListarrayList of images to render.
onImageUploadfunctionCalled when an element is clicks and triggers to open a file dialog
onImageRemoveAllfunctionCalled when removing all images
onImageUpdate(updateIndex: number) => voidCalled when updating an image at updateIndex.
onImageRemove(deleteIndex: number | number[]) => voidCalled when removing one or list image.
errorsobjectExported object of validation
dragPropsobjectNative element props for drag and drop feature
isDraggingboolean"true" if an image is being dragged

Contributors ✨

Thanks goes to these wonderful people (emoji key):


vutoan266

💻 🤔 📖 🚧 ⚠️ 👀

David Nguyen

💻 🤔 📖 👀

DK

💻 🚇 🤔 👀 📖

Isaac Maseruka

💻

This project follows the all-contributors specification. Contributions of any kind welcome!

Keywords

FAQs

Package last updated on 31 Jul 2021

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