How to using React Hooks to Upload Files to Firebase

How to using React Hooks to Upload Files to Firebase
This is a sample app integrating Firebase with a React application using React Hooks api and React Firebase Hooks — A set of reusable React Hooks for Firebase. The custom Hook developed in this post was enhanced to support additional functionality.


This is the second in a series of pieces about Ionic Framework, React Hooks, and Firebase.

In this piece, I’m walking through the process of creating a custom Hook for uploading a file to Firebase.

Since the focus of the piece is the custom Hook, I will focus on pieces of code related to the Hook, how it is called and how it is implemented, and not the surrounding code; however, the full source code for the complete project is provided on GitHub.

Setting Up Parent Component

// custom hook that will upload to firebase
import useFirebaseUpload from "../hooks/useFirebaseUpload";

We need to make sure we set things up by initializing the custom file upload Hook useFirebaseUpload as follows:

// setting up the hook to upload file and track its progress
  const [
    { data, isLoading, isError, progress },
  ] = useFirebaseUpload();

Next, in the parent component, we want to present any errors that are generated and get progress information when the file is being uploaded from the custom file upload Hook useFirebaseUpload. The following properties are all reactive and provided by the custom Hook: isError, isLoading and progress.

  {/* get error from hook and display if necessary */}
  {isError && <div>ERROR: {isError.message}</div>}

  {/* get loading info from hook & display progress if necessary */}
  {isLoading && progress && (
    <IonProgressBar value={progress.value}></IonProgressBar>
  ) }

The last missing piece for the parent component is selecting the file and then calling the method on the custom Firebase Hook to upload the file. We handle that with the code listed below.

Calling that function will set a property in the Hook that is a dependency for the useEffects handler we set that actually triggers the Firebase upload to start.

{/* user selects a file and returns the info required for upload */}
    onChange={(e: any) => {

Inside Custom Firebase File Upload Hook

Setting things up

We will initialize Firebase at the start of the component function and define a reference to the storage to be used throughout the component function.

Add Firebase to your JavaScript project

var firebaseConfig = {
// Initialize Firebase
// the firebase reference to storage
const storageRef =;

Since we are using typescript, we need to define some interfaces for use in the Hook, and we define the return type from the Hook function.

interface UploadDataResponse { 
   downloadUrl: any 
interface ProgressResponse { value: number }

function FirebaseFileUploadApi(): [{
    data: UploadDataResponse | undefined,
    isLoading: boolean,
    isError: any,
    progress: ProgressResponse | null
] { //additional code... }

Next we start to define the state variables needed by the Hook.

// the data from the firebase file upload response
const [data, setData] = useState<UploadDataResponse | undefined>();

// sets properties on the file to be uploaded, this is called
// by the parent component
const [fileData, setFileData] = useState<File | null>();

// if we are loading a file or not
const [isLoading, setIsLoading] = useState<boolean>(false);

// if an error happened during the process
const [isError, setIsError] = useState<any>(false);

// used for tracking the % of upload completed
const [progress, setProgress] = useState<ProgressResponse | null>(null);

The useEffect handler

useEffect is called after every render of the component. There is a way to control the render by providing an array of dependencies as the second parameter.

With our Hook, we only want it to be called when the fileData property changes, meaning that the user has selected a file to upload and indicated that by calling the setData method.

// this function will be called when the any properties in the dependency array changes
useEffect(() => {
    const uploadData = async () => {
        // initialize upload information

        setProgress({ value: 0 });

        if (!fileData) return;

        // wrap in a try catch block to update the error state
        try {
            let fName = `${(new Date()).getTime()}-${}`

            // setting the firebase properties for the file upload
            let ref = storageRef.child("images/" + fName);
            let uploadTask = ref.put(fileData);

            // tracking the state of the upload to update the
            // application UI
            // method details covered in the next section...
                _progress => { },
                _error => { },
                async () => { }
        } catch (_error) {

    fileData && uploadData();
}, [fileData]); 

Manage Firebase File Upload State Changes

The call to upload the file, ref.put(fileData), returns a property that we can use to monitor the state of the upload for errors, for progress updates, and for when it completes.

We have included a handler for each one and set the appropriate state variable to be accessible from the Hook. We will dig a bit deeper on the completion handler because we need to make another call into Firebase uploadTask.snapshot.ref.getDownloadURL() to get the downloadUrl, which is needed to render the image in the application.

// tracking the state of the upload to assist in updating the
// application UI

    _progress => {
        var value =
            (_progress.bytesTransferred / _progress.totalBytes);
        console.log("Upload is " + value * 100 + "% done");
        setProgress({ value });
    _error => {
    async () => {

        // need to get the url to download the file
        let downloadUrl = 
               await uploadTask.snapshot.ref.getDownloadURL();

        // set the data when upload has completed
            metaData: uploadTask.snapshot.metadata,

        // reset progress

Wrapping Up

Basic example

This is a very basic file upload component using Firebase. I have created a separate GitHub repo for this project where I have excluded login, create account, and other features that you would expect to find; I felt it was important to keep the code simple.

Full Source Code

Ionic custom Hooks and Capacitor example

As I was wrapping this piece, I saw that the team from Ionic had released a blog post about custom Hooks Announcing Ionic React Hooks. To see the Firebase file upload Hook integrated with Ionic Framework and Capacitor, see this branch in the GitHub repo.

Integration with Capacitor Custom Hooks

Complete Firebase Hooks example In React

This is a sample app integrating Firebase with a React application using React Hooks api and React Firebase Hooks — A set of reusable React Hooks for Firebase. The custom Hook developed in this post was enhanced to support additional functionality.

Complete Example with Authentication, Collections, File Upload, CRUD Actions


Full Stack React & Firebase Tutorial - Build and Launch Real-World App

Top 11 React UI Component Libraries In 2018

Learn React.js - Full Course for Beginners

Learn the complete React-Redux front-end system

Using Typescript with modern React (i.e. hooks, context, suspense)

React Fundamentals for Beginners