By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – Populating a React component with data from Firebase
JavaScript

Javascript – Populating a React component with data from Firebase

Admin
Last updated: 2024/01/07 at 6:49 AM
Admin
Share
2 Min Read
Populating a React component with data from Firebase

Problem:

I’m trying to populate a React component using data available in Firestore.
I can get a console.log of the data without any problems, however, the dynamic element population doesn’t happen, either by calling the Promise directly or through useEffect.

Contents
Problem:Solution:

The only time I have it worked is when I add an onclick event on the element and taking it away after the first click.

const OptionList = () => {

    const [list, setList] = useState({})
      
    const fetchData = () => {
        let data = []; 
        const response = getDocs(collection(db, 'listing'));

        response.then((results) => {
            let id = 1;
            results.forEach(function(doc) {
                const tempData = {id:id, name: doc.id, active: doc.data().active}
                id+=1;
                data.push(tempData);
            });
        })
        setList(data)
    }

    // useEffect(() => {
    //   fetchData()
    // }, []);  



    return(
        <>
            <select name="collection" id="collection" className="w-full rounded-lg border-2 border-gray-200 p-3 placeholder-zinc-500" >
                <option value="new">New Collection</option>
                {Object.values(list).map((item) => <option key={item.id} value={item.name}>{item.name}</option> )} 
                {Object.values(list).forEach((item) => console.log(item) )} 
            </select>
        </>
    )
}

Any indication of where to issues lie is very much appreciated.

Solution:

Put the call to setList inside the promise callback. Right now it’s always setting an empty list before the database results are hanlded, because promise callbacks are always asynchronous.

        response.then((results) => {
            let id = 1;
            results.forEach(function(doc) {
                const tempData = {id:id, name: doc.id, active: doc.data().active}
                id+=1;
                data.push(tempData);
            });
            setList(data)
        })

If you add more logging in and around the callback you’ll be able to better see the order in which everything executes.

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article Property 'includes' does not exist on type 'RouteRecordName' Javascript – Property ‘includes’ does not exist on type ‘RouteRecordName’
Next Article Writing a character by keyPress in the textarea by JavaScript or jQuery (permission to send a message on chat.openai.com via Google Chrome extension) [duplicate] Javascript – Writing a character by keyPress in the textarea by JavaScript or jQuery (permission to send a message on chat.openai.com via Google Chrome extension) [duplicate]
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?