How to list all the files from firebase storage using ReactJS ?

30 Jun, 2021
The following approach covers how to list all the files from firebase storage using react. We have used the firebase module to achieve so.
Creating React Application And Installing Module:
Step 1: Create a React-app using the following command:
npx create-react-app myapp
Step 2: After creating your project folder i.e. myapp, move to it using the following command:
cd myapp
 
Project structure: Our project structure will look like this.
Step 3: After creating the ReactJS application, Install the firebase module using the following command:
npm install firebase@8.3.1 --save
Step 4: Go to your firebase dashboard and create a new project and copy your credentials.
const firebaseConfig = {
      apiKey: "your api key",
      authDomain: "your credentials",
      projectId: "your credentials",
      storageBucket: "your credentials",
      messagingSenderId: "your credentials",
      appId: "your credentials"
};
Step 5: Initialize the Firebase into your project by creating a firebase.js file with the following code.
firebase.js
import firebase from 'firebase';  const firebaseConfig = {    // Your Credentials};  firebase.initializeApp(firebaseConfig);var storage = firebase.storage();  export default storage;
Step 6: Now go to your storage section in the firebase project and update your security rules. Here we are in testing mode, so we allow both read and write as true. After updating the code shown below, click on publish. 
Step 7: Now implement the list part. Here, We are going to use a method called listAll which helps us to get the list of all the files from firebase storage.
App.js
import { useState } from 'react';import storage from './firebase';  function App() {    // States for data and image  const [data, setData] = useState([]);  const [image, setImage] = useState('');    const upload = () => {    if (image == null)      return;    // Sending File to Firebase Storage    storage.ref(`/images/${image.name}`).put(image)      .on("state_changed", alert("success"), alert);  }    // List All Files  const listItem = () => {    storage.ref().child('images/').listAll()      .then(res => {        res.items.forEach((item) => {          setData(arr => [...arr, item.name]);        })      })      .catch(err => {        alert(err.message);      })  }    return (    <div className="App" style={{ marginTop: 250 }}>      <center>        <input type="file" onChange={(e) => {          setImage(e.target.files[0])        }} />        <button onClick={upload}>Upload</button>        <br /><br /><br /><br /><br /><br />        <button onClick={listItem}>List Item</button>        <br /><br />        {          data.map((val) => (            <h2>{val}</h2>          ))        }      </center>    </div>  );}  export default App;
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:
Firebase
React-Questions
ReactJS
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Axios in React: A Guide for Beginners
ReactJS setState()
How to pass data from one component to other component in ReactJS ?
Re-rendering Components in ReactJS
ReactJS defaultProps
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Installation of Node.js on Linux
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
Differences between Functional Components and Class Components in React