Make a download button using Reactjs

share link

by vsasikalabe dot icon Updated: Feb 21, 2023

technology logo
technology logo

Solution Kit Solution Kit  

Call the download API for the file to download a BLOB with an onclick of the button. For storing the object downloaded in the browser memory, create a URL out of the blob. To link the above-generated URL, Click the hidden anchor tag programmatically. The Node interface method is the appendChild(). To add a node to the child node list's end, then we will use the appendChild() method. The child node is appended to the given parent node in this method. 

We need four things to download a file: 

  • The content of the file 
  • The file object  
  • A link to download the file object 
  • Finally, simulate the user clicking the link 

The appendChild() returns the appended child. An existing object URL was released by the URL revokeObjectURL() method, created using URL createObjectURL(). Suppose you finished using an object URL and don't want the browser to keep the reference to that file longer. The HTML document was written directly for SVG images. Please copy the code and paste it inside the element in your HTML document to open the SVG image in VS code or your preferred IDE.  

When the download button is clicked to trigger the browser to download an SVG file, I reach into the browser and create a tag with document.createElement. Here is a breakdown: 

  •  First, we must set the anchor's href attribute with a method called encodeURIComponent(), which takes as a parameter whatever you want the body of your downloaded file to contain (in this case, i.e., an SVG document). 
  • Then set the anchor's download attribute. This tells our DOM that this is a link to a file download (not a URL), and it also sets the files' filename.  
  • By hiding it with display = "none", ensure the anchor doesn't cause a mess on our DOM. 
  • Finally, I append our temporary file anchor to the DOM with appendChild(), simulate a click() to trigger a download, and clean up by removing it from the DOM. 
  • Add an onClick to our download button in the React component. 

Here is an example of how to make a download button using Reactjs: 

Preview of the output that you will get on running this code from your IDE.


In this solution we use the React library.


Follow the steps carefully to get the output easily.

  1. Install the Node.js and React on your IDE(preferable Visual Studio Code).
  2. Create React Application using npx create-react-app foldername.
  3. cd foldername
  4. npm i @babel/runtime
  5. Open the folder in IDE.
  6. Copy the code using "copy" button above and paste it in App.js file and also add path and size of the image within the svg tag.(remove the earlier code from App.js).
  7. import { useCallback, useRef } from "react"
  8. Refer DEMO for additional files.
  9. Open the terminal from IDE.
  10. npm start to run the file.

You can also refer this url DEMO for getting the above output.

I hope you found this useful. I have added the link to dependent libraries, version information in the following sections.

I found this code snippet by searching for Make a download button using Reactjs in kandi. You can try any such use case!

Environment Tested

I tested this solution in the following versions. Be mindful of changes when working with other versions.

  1. The solution is created in VS Code 1.73.1 version.
  2. The solution is tested on Nodejs 16.14.2 version.
  3. react 18.2.0 version.
  4. @babel/runtime: 7.20.6 version.

Using this solution, we are able to make a download button using Reactjs with simple steps. This process also facilities an easy way to use, hassle-free method to create a hands-on working version of code which would help us to make a download button using Reactjs.

Dependent Libraries

create-react-appby facebook

JavaScript doticonstar image 100082 doticonVersion:v5.0.1doticon
License: Permissive (MIT)

Set up a modern web app by running one command.


            create-react-appby facebook

            JavaScript doticon star image 100082 doticonVersion:v5.0.1doticon License: Permissive (MIT)

            Set up a modern web app by running one command.

                      You can search for any dependent library on kandi like react.


                      1. For any support on kandi solution kits, please use the chat
                      2. For further learning resources, visit the Open Weaver Community learning page.

                      See similar Kits and Libraries