0

i am stuck at this point leaving me to ask this question here : my firebase storage API triggers twice for some reasons here is my app js

import "./App.css";
import { useEffect, useState } from "react";
import { storage } from "./firebase";
import {
  ref,
  getDownloadURL,
  uploadBytesResumable,
  listAll,
} from "firebase/storage";

function App() {
  const [imgUrl, setImgUrl] = useState(null);
  const [progresspercent, setProgresspercent] = useState(0);
  const imagesListRef = ref(storage, "images/");
  const [imageUrls, setImageUrls] = useState([]);
  const handleSubmit = (e) => {
    e.preventDefault();
    const file = e.target[0]?.files[0];
    if (!file) return;
    const storageRef = ref(storage, `images/${file.name}`);
    const uploadTask = uploadBytesResumable(storageRef, file);

    uploadTask.on(
      "state_changed",
      (snapshot) => {
        const progress = Math.round(
          (snapshot.bytesTransferred / snapshot.totalBytes) * 100
        );
        setProgresspercent(progress);
      },
      (error) => {
        alert(error);
      },
      () => {
        getDownloadURL(uploadTask.snapshot.ref).then((downloadURL) => {
          setImgUrl(downloadURL);
        });
      }
    );
  };
  useEffect(() => {
    listAll(imagesListRef).then((response) => {
      response.items.forEach((item) => {
        getDownloadURL(item).then((url) => {
          setImageUrls((prev) => [...prev, url]);
        });
      });
    });
  }, []);

  return (
    <div className="App">
      <form onSubmit={handleSubmit} className="form">
        <input type="file" />
        <button type="submit">Upload</button>
      </form>
      {!imgUrl && (
        <div className="outerbar">
          <div className="innerbar" style={{ width: `${progresspercent}%` }}>
            {progresspercent}%
          </div>
        </div>
      )}
      {imgUrl && <img src={imgUrl} alt="uploaded file" height={200} />}
      {imageUrls.map((url) => {
        return <img src={url} />;
      })}
    </div>
  );
}
export default App;

and here is my firebase.js :

import { initializeApp } from "firebase/app";
import { getStorage } from "firebase/storage";
const firebaseConfig = {
  apiKey: "AIzaSyAG9YvHxwKIr3FdL58KNkAGWUPVOFoNfn8",
  authDomain: "leaning-firebase1888.firebaseapp.com",
  projectId: "leaning-firebase1888",
  storageBucket: "leaning-firebase1888.appspot.com",
  messagingSenderId: "1075477563305",
  appId: "1:1075477563305:web:41b79ff97684d4e67d26b8",
};

export const app = initializeApp(firebaseConfig);
export const storage = getStorage(app);

i read a answer on stackoverflow but did not understand please help what am i doing wrong here ???

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
  • Does this answer your question? [useEffect is running twice on mount in React](https://stackoverflow.com/questions/72238175/useeffect-is-running-twice-on-mount-in-react) – Youssouf Oumar Oct 04 '22 at 10:29
  • i actually did not understand ANYTHING and very thanks for replying – Dev Contractor Oct 05 '22 at 12:28
  • can you verify whether useEffect executes twice? you can identify that easily by adding a console.log('useEffect executed') and observe the browser's javascript console . You should only see 1 "useEffect executed" message appearing. – Lim Shang Yi Oct 05 '22 at 16:16
  • yeah i actually just started a FRESH code and then a new firebase project and it worked i have no idea but it did and really thank you very much for staying this long with me i really appreiciate it ❤❤ – Dev Contractor Oct 06 '22 at 11:27

0 Answers0