refactored updated version:
import React, { useState } from "react"; import firebase from "firebase/compat/app"; import "firebase/compat/storage"; export const storage = firebase.storage(); export default function App() { const [file, setFile] = useState(null); const [url, setURL] = useState(""); function handleChange(e) { if (e.target.files[0]) setFile(e.target.files[0]); } function handleUpload(e) { e.preventDefault(); const path = `/images/${file.name}`; const ref = storage.ref(path); await ref.put(file); const url = await ref.getDownloadURL(); setURL(url); setFile(null); } return ( <div> <form onSubmit={handleUpload}> <input type="file" onChange={handleChange} /> <button disabled={!file}>upload to firebase</button> </form> <img src={url} alt="" /> </div> ); }
it gives error storage.ref is not a function
i've updated the snippet for new firebase >= 9.0
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
refactored updated version:
it gives error
storage.ref is not a function
i've updated the snippet for new firebase >= 9.0