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

141
Views
React cant use useParams values

So my code was working with no issues until i added useParams

[...]

const promiseDelay = () => {
    const E_COMMERCE_API = "https://mocki.io/v1/027ebfda-9bd8-4b53-ae33-ab41586d773a";
    return new Promise((resolve, reject) => {
        setTimeout(() => resolve(fetch(E_COMMERCE_API)), 1000);
    });
};

export default function ItemDetailContainer() {
    const { itemId } = useParams();
    const [dataToShow, setDataToShow] = useState([]);

    useEffect(() => {
        promiseDelay()
            .then((response) => response.json())
            .then((data) => {
                const aux = data.find((element) => {
                    console.log(itemId); //<--- 2
                    // return element.id === itemId; <--- if i uncomment, i get no data
                    return element.id === 2; // with this line, everything works just fine
                });
                setDataToShow(aux);
            });
    }, []);

    [...]
}

Apparently even if itemId its 2 its not the same and wrecks everything.

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

0

It's probably because the param is a string when it's parsed from the URL. The useParams hook doesn't know to parse it to a number.

So element.id === Number(itemId) should fix it.

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!