'React-Dropzone how convert each file to base64

I am using react-dropzone plugin for file uploads. I worried how i can convert each file to base64:

eg:

Here is my function where i get files: I am creating here for example thumb for each file and attach to object. But how add to item here prop like base64string: and it will keep base64 data for each file?

this.onDrop = files => {
      files.map(file =>
        Object.assign(file, {
          preview: URL.createObjectURL(file),
        })
      );
    };


Solution 1:[1]

check this out,you can get files and then you can store them into images array of state instance.

onDropHandler = (files) => {
    files.map(file => {
        const reader = new FileReader();
        reader.onload = (event) => {
            //store result into your state array.
            this.setState(prevState => {
                const updatedImages = [...prevState.images, event.target.result];
                return {
                    images: updatedImages,
                }
            })
            console.log(event.target.result);
        };
        reader.readAsDataURL(file);
    });
}

Solution 2:[2]

With this code you get an array with the base64 data and blob url for the preview.

const [data, setData] = useState([])

const onDrop = (files) => {
files.map((file) => {
  const reader = new FileReader()
  reader.onload = (e) => {
    setdata(
      files.map((file) =>
        Object.assign(file, {
          preview: URL.createObjectURL(file),
          base64Image: e.target.result,
        })
      )
    )
  }
  reader.readAsDataURL(file)
})

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Smit Vora
Solution 2 user3785328