How to Create Image Uploader App with React and Firebase

Ayumi Tanaka
6 min readApr 18, 2021

This article explains how to use Firebase in React to upload image files.

1. Create a Firebase Project

First thing first, let’s create a firebase project.

  1. Sign up for Firebase, if you have a Google account you can sign up right away.
  2. Add project.
  3. Name your project.
  4. Add Google Analytics. If you don’t need Google Analytics, it’s totally fine.
  5. Configure Google Analytics. You may choose “Default Account for Firebase”.
  6. Nicely done!
2
3
4
5
6

2. Create a React App

Second, let’s create your react app.

  1. create-react-app {your-app-name} --use-npm
  2. cd {your-app-name}

3. Create a Firebase Storage

Third, create firebase storage on your project.

  1. Go to Storage from the sidebar, then click “Get started”.
  2. You will see a pop-up about rules. Click “Next” to continue.
  3. Select a location for Cloud Storage. Note that the location cannot be changed later, but for now, you may select the closest location.
  4. Go to Rules. Default is if request.auth != null; but you may change it to if true; for the testing. We should be able to authenticate the user before writing the upload process but to test the upload process, we will allow anyone to upload without authentication.
1
2
3
4

4. Create a Web App

Next, let’s go back to the top page.

  1. Add a web app.
  2. Name your app. Check “Also set up Firebase Hosting for this app”.
  3. You’ll need Firebase SDK Config so don’t need to copy it. You can check SDK Config later.
  4. Install Firebase CLI. You may type npm install -g firebase-tools on your terminal. You might have used Firebase before, in this case, you can check the version with npm info firebase-tools version.
  5. Deploy to Firebase Hosting. It is a little bit confusing part. So I’ll write it down in detail.
1
2
3
4

I’ve already installed firebase-tools, so just checked the version.

5

Once you are done installing “firebase-tools”, you may type this.

firebase login

After you successfully logged in, you may type this.

firebase init

Then you will see this.

You will be asked some questions. First, select “Hosting” and “Storage”.

Second, this is about project setup. Select “Use an existing project”, and choose the project that you created in the beginning.

Third, this is about hosting setup.

? What do you want to use as your public directory? (public)

Enter build

? Configure as a single-page app (rewrite all urls to /index.html)? (y/N)

Enter No

? Set up automatic builds and deploys with GitHub?

Either is fine. I entered no in this time.

Lastly, this is about storage setup.

? What file should be used for Storage Rules? (storage.rules)

Enter just enter key.

Now you’re done!

5. Install Firebase in React App

Let’s get back to your code editor.

  1. To use firebase in React app, install the firebase package. You may type npm install firebase. Once you finish installing, you can check in package.json.
1

6. Create a Firebase Config File

Do you remember I wrote you can check SDK Config later in section 4? Now the time to check the SDK config!

  1. Create a directory named “firebase” in the src directory.
  2. Create a file named “firebase.js” in the firebase directory.
  3. In firebase.js, you may write like this.
  4. Go to the firebase console, and check your SDK config from “Project Setting”. Then go to “Firebase SDK snippet”, and select “Config”.
  5. Let’s get back to your code editor. Create a file named “.env” in your app directory. (Same place as where package.json in.)
  6. In .env, you may write like this. I hid my config SDK for privacy's sake, but you will write your config SDK from the firebase console.
  7. If you want to push this app on GitHub, make sure to add the “.env” in the “.gitignore” file.
3
4
6

7. Create a Form

The setting part is almost done. Let’s create a form!

  1. Open App.js and delete unnecessary code. You could delete the code inside the orange square.
  2. Import React, useState, firebase, and storage.
  3. Create a form inside <div className="App></div>.
  4. Create two useState, one is about the image, another is about the URL.
  5. Create event handle functions. Create “onSubmit” and “onChange”.
  6. Create a function to get a URL of firebase storage.
1
2
3
4
5
6

If you’ll see this screen after this section, you are successfully done!

Final screen

8. Test

Let’s test whether this app will work or not. After uploading the image, you’ll see the image below the input.

Once you check the image, go to the firebase console. Check storage if there’s an image.

9. Deploy App

You successfully created the image uploader app! So now you could deploy on Firebase hosting.

  1. Build your files with npm run build.
  2. Deploy your app with firebase deploy.

Then you can check it’ll work or not globally!

9. Sources

--

--