A
A
Alexander2021-05-08 20:39:03
React
Alexander, 2021-05-08 20:39:03

How to display multiple images in ReactJS?

How to display multiple images in ReactJS
Single load example below, how can I select multiple images at once and then display them in ui while
keeping blobUrl in useState for further sending to server, any help would be greatly appreciated

interface ImageObj {
  blobUrl: string
  file: File
}
  const [images, setImages] = React.useState<ImageObj[]>([])

  const onSelectImage = React.useCallback((e: ChangeEvent<HTMLInputElement>) => {
    if (e.target.files?.length) {
      const file = e.target.files[0]
      if (file) {
        const fileObj = new Blob([file])
        setImages((prev) => [
          ...prev,
          {
            blobUrl: URL.createObjectURL(fileObj),
            file,
          },
        ])
      }
    }
  }, [])

<input
                accept='image/*'
                style={{ display: 'none' }}
                id='icon-button-file'
                type='file'
                onChange={(e) => {
                  onSelectImage(e)
                }}
                disabled={images.length >= 4}
              />

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question