Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

useParams and useEffect, Parameter via ${id} in the useEffect function

This is the code, I receive the id in the console but not the response from the firestore server

import Item from './Item'
import { useState, useEffect } from "react";
import { useParams } from 'react-router-dom';
import {db} from "../Firebase"
import { collection, getDocs, query,} from "firebase/firestore";


const ContainerItems = () => {

    let idParam = useParams();
    const [ dataItem, setDataItem ] = useState([])


useEffect( () => {
    async function fetchData(){
    const querySnapshot = await getDocs(query(collection(db, `category/${(idParam)}/Items` )));
      let dataArray = []
      querySnapshot.forEach((doc) => {
        dataArray.push({...doc.data(), id: doc.id});
      });
      setDataItem(dataArray)
      console.log("Id",idParam)
    }
    fetchData();
}, [idParam])
    return (
        <>
            {dataItem.map((data)=>( 
                <Item item={data} key={data.id}/>
            ))}            
        </>
    )
}

export default ContainerItems;

the request to the server works, it just doesn’t work when I add ${idParam}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to get the idParam out of the object using destructuring.

let { idParam }  = useParams();

NOTE: Assuming you have named the path in the Route with the same name.

<Route path="/category/:idParam">...</Route>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading