Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

93
Views
How to do dropdowns with React

I'm trying to make a dropdown with a react component and the anchor tags do not render. The anchors should be on the dropdown when clicked, however they don't appear. If getItems() fails, the default dropdown should just say loading. Any suggestions about the dropdown?

Dropdown.js

<li className="nav-item dropdown">
  <a className="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    <img src={shoppingCart} alt="shopping cart" className="" style={{ width: "30px", height: "30px"}}></img>
  </a>
  <div className="dropdown-menu" aria-labelledby="navbarDropdown">
    <CartItems></CartItems>
  </div>
</li>

CartItems.js

export default function CartItems() {

    const { currentUser } = useAuth();
    const [itemsRef, setItemsRef] = useState([]);


    async function getItems() {
        if(currentUser !== null) {
            const q = query(collection(db, currentUser.uid));
            console.log('q', q);
            const querySnapshot = await getDocs(q);
            console.log("snapshot", querySnapshot);
            setItemsRef(querySnapshot);
            console.log('items', itemsRef)
            querySnapshot.forEach((doc) => {
                // doc.data() is never undefined for query doc snapshots
                console.log(doc.id, " => ", doc.data());
            });
        }
       
    }

    useEffect(() => {
        getItems();
    }, [])

    if(itemsRef !== undefined && itemsRef !== null && itemsRef.length !== 0) {
    return(
        <>
            {
                itemsRef.forEach((item, index) => {
                    return <CartItem key={index} item={item} />
                })
            }
        </>
    );
    } else {
        return <a className="dropdown-item" href="#">Loading Cart...</a>
    }
}

CartItem.jsx

class cartItem extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            
        }
    }
    render() { 
        return <a className="dropdown-item" href="#">
            <img src={this.props.item.image} style={{width: "10px", height: "17px"}}></img>
            <p>{this.props.item.title}</p>
            <button ></button>
        </a>
    }
}
 
export default cartItem;

Any reason this wouldn't work?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The problem is that querySnapshot is an object and needs to be converted into an array.

async function getItems() {
        if(currentUser !== null) {
            const q = query(collection(db, currentUser.uid));
            console.log('q', q);
            const querySnapshot = await getDocs(q);
            console.log("snapshot", querySnapshot);
            
            let array = [];
            querySnapshot.forEach((doc) => {
                // doc.data() is never undefined for query doc snapshots
                console.log(doc.id, " => ", doc.data());
                array.push(doc.data());
            });
            // console.log(array);
            setItemsRef(itemsRef => [...itemsRef, array]);
            console.log('items', itemsRef);
        }
       
    }

And the dropdown need to be done with react bootstrap

import Dropdown from 'react-bootstrap/Dropdown'

<Dropdown.Menu>
    <CartItem />
</Dropdown.Menu>

Where CartItem countains a <Dropdown.Item>.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!